我正在尝试使用org.apache.http api编写带有SOAP操作的硬编码HTTP Post请求。 我的问题是我没有找到添加请求体的方法(在我的情况下 - SOAP动作)。 我很乐意提供一些指导。
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.protocol.HTTP;
public class HTTPRequest
{
@SuppressWarnings("unused")
public HTTPRequest()
{
try {
HttpClient httpclient = new DefaultHttpClient();
String body="DataDataData";
String bodyLength=new Integer(body.length()).toString();
System.out.println(bodyLength);
// StringEntity stringEntity=new StringEntity(body);
URI uri=new URI("SOMEURL?Param1=1234&Param2=abcd");
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Test", "Test_Value");
// httpPost.setEntity(stringEntity);
StringEntity entity = new StringEntity(body, "text/xml",HTTP.DEFAULT_CONTENT_CHARSET);
httpPost.setEntity(entity);
RequestWrapper requestWrapper=new RequestWrapper(httpPost);
requestWrapper.setMethod("POST");
requestWrapper.setHeader("LuckyNumber", "77");
requestWrapper.removeHeaders("Host");
requestWrapper.setHeader("Host", "GOD_IS_A_DJ");
// requestWrapper.setHeader("Content-Length",bodyLength);
HttpResponse response = httpclient.execute(requestWrapper);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:8)
这是一个完整的工作示例:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public void callWebService(String soapAction, String soapEnvBody) throws IOException {
// Create a StringEntity for the SOAP XML.
String body ="<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://example.com/v1.0/Records\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><SOAP-ENV:Body>"+soapEnvBody+"</SOAP-ENV:Body></SOAP-ENV:Envelope>";
StringEntity stringEntity = new StringEntity(body, "UTF-8");
stringEntity.setChunked(true);
// Request parameters and other properties.
HttpPost httpPost = new HttpPost("http://example.com?soapservice");
httpPost.setEntity(stringEntity);
httpPost.addHeader("Accept", "text/xml");
httpPost.addHeader("SOAPAction", soapAction);
// Execute and get the response.
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String strResponse = null;
if (entity != null) {
strResponse = EntityUtils.toString(entity);
}
}
答案 1 :(得分:4)
soapAction必须作为http-header参数传递 - 使用时,它不是http-body / payload的一部分。
在这里查看apache httpclient:http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/PostSOAP.java
的示例答案 2 :(得分:4)
... using org.apache.http api. ...
您需要在请求中包含SOAPAction
作为标头。由于您有httpPost
和requestWrapper
句柄,因此添加标题有三种方法。
1. httpPost.addHeader( "SOAPAction", strReferenceToSoapActionValue );
2. httpPost.setHeader( "SOAPAction", strReferenceToSoapActionValue );
3. requestWrapper.setHeader( "SOAPAction", strReferenceToSoapActionValue );
唯一的区别是addHeader
允许具有相同标题名称的多个值,而setHeader
仅允许唯一标题名称。 setHeader(...
over写出具有相同名称的第一个标头。
您可以根据自己的要求选择其中任何一项。
答案 3 :(得分:0)
通过Java客户端调用WCF服务时,确定需要在soap操作上设置的内容的最简单方法是加载wsdl,转到与服务匹配的操作名称。从那里拿起动作URI并在soap动作头中设置它。你完成了。
例如:来自wsdl
<wsdl:operation name="MyOperation">
<wsdl:input wsaw:Action="http://tempuri.org/IMyService/MyOperation" message="tns:IMyService_MyOperation_InputMessage" />
<wsdl:output wsaw:Action="http://tempuri.org/IMyService/MyServiceResponse" message="tns:IMyService_MyOperation_OutputMessage" />
现在在java代码中我们应该将soap操作设置为Action URI。
//The rest of the httpPost object properties have not been shown for brevity
string actionURI='http://tempuri.org/IMyService/MyOperation';
httpPost.setHeader( "SOAPAction", actionURI);
答案 4 :(得分:0)
它将 415 Http响应代码视为错误,
所以我添加了
httppost.addHeader("Content-Type", "text/xml; charset=utf-8");
现在一切都好了,Http:200
答案 5 :(得分:0)
这是我尝试过的示例,它对我有用:
创建XML文件SoapRequestFile.xml
stack
这里是Java中的代码:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetConversionRate>
<!--Optional:-->
<tem:CurrencyFrom>USD</tem:CurrencyFrom>
<!--Optional:-->
<tem:CurrencyTo>INR</tem:CurrencyTo>
<tem:RateDate>2018-12-07</tem:RateDate>
</tem:GetConversionRate>
</soapenv:Body>
</soapenv:Envelope>