利用HTTPCLIENT调用WEBSERVICE接口,axis1.4报no SOAPAction header!解决

本文介绍了一种解决使用HTTPCLIENT调用WEBSERVICE接口时遇到的noSOAPActionheader错误的方法。通过添加SOAPAction头解决了axis1.4版本的一个已知问题,并提供了一个示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用HTTPCLIENT调用WEBSERVICE接口,结果出现报错no SOAPAction header!

百度查了下,说是axis1.4的bug,于是反编译查看了源码,发现需要获取header SOAPAction,于是加了就可以成功了

代码中的1.xml实际上是soap的报文

例如:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.autotest.asiainfo.com">
   <soapenv:Header/>
   <soapenv:Body>
      <web:queryUserById>
         <web:userId>1111</web:userId>
      </web:queryUserById>
   </soapenv:Body>
</soapenv:Envelope>

 

java代码如下:

        Map<String,String> map = new HashMap<String,String>();
        try {
            httpClient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.7.5.56:1808/AutoTestWeb/services/AutoTestWebservice?wsdl"); //webservice服务地址
            String xml = FileUtils.readFileToString(new File("e:/1.xlml"));//
            String soapRequestData = xml; //soap协议的格式,定义了方法和参数
            HttpEntity re = new StringEntity(soapRequestData, HTTP.UTF_8);
            httppost.setHeader("Content-Type","application/soap+xml; charset=utf-8");
            httppost.setHeader("SOAPAction","application/soap+xml; charset=utf-8");//随便填,必须要有值
            httppost.setEntity(re);
            HttpResponse response = httpClient.execute(httppost); //调用接口
            System.out.println(response.getStatusLine().getStatusCode());
            if(response.getStatusLine().getStatusCode() == 200) {  //调用状态
                String xmlString = EntityUtils.toString(response.getEntity());
                System.out.println("===="+xmlString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient.getConnectionManager().shutdown(); //关闭连接
        }
    }
    

在 C# 中使用 `HttpClient` 调用 WebService 接口的方法如下: 1. 首先需要添加 `System.Net.Http` 和 `System.Web.Services` 引用。 2. 在代码中创建 `HttpClient` 实例,并设置请求头信息和请求内容。 ```csharp using System; using System.Net.Http; using System.Threading.Tasks; using System.Xml; namespace ConsoleApp1 { class Program { static async Task Main(string[] args) { // 创建 HttpClient 实例 HttpClient httpClient = new HttpClient(); // 设置请求头信息 httpClient.DefaultRequestHeaders.Add("SOAPAction", "http://tempuri.org/HelloWorld"); // 设置请求内容 string soapRequest = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <HelloWorld xmlns=""http://tempuri.org/"" /> </soap:Body> </soap:Envelope>"; HttpContent httpContent = new StringContent(soapRequest, System.Text.Encoding.UTF8, "text/xml"); // 发送请求 HttpResponseMessage httpResponse = await httpClient.PostAsync("http://localhost:8080/HelloWorldService.asmx", httpContent); // 处理响应 if (httpResponse.IsSuccessStatusCode) { string soapResponse = await httpResponse.Content.ReadAsStringAsync(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(soapResponse); string result = xmlDoc.GetElementsByTagName("HelloWorldResult")[0].InnerText; Console.WriteLine($"调用成功,返回结果:{result}"); } else { Console.WriteLine($"调用失败,响应状态码:{httpResponse.StatusCode}"); } } } } ``` 上述代码以调用 HelloWorld 接口为例,可以根据实际情况更改请求头信息和请求内容。此外,我们还需要处理响应。如果响应状态码为成功,我们可以解析出响应内容,并获取接口返回的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值