使用 org.apache.http 发送带有 SOAP 操作的 HTTP Post 请求 [英] Sending HTTP Post request with SOAP action using org.apache.http
问题描述
我正在尝试使用 org.apache.http api 编写带有 SOAP 操作的硬编码 HTTP Post 请求.我的问题是我没有找到添加请求正文的方法(在我的情况下 - SOAP 操作).我很乐意为您提供指导.
I'm trying to write a hard-coded HTTP Post request with SOAP action, using the org.apache.http api. My problem is I didn't find a way to add a request body (in my case - SOAP action). I'll be glad for some guidance.
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();
}
}
}
推荐答案
soapAction 必须作为 http-header 参数传递 - 使用时,它不是 http-body/payload 的一部分.
The soapAction must passed as a http-header parameter - when used, it's not part of the http-body/payload.
在此处查看 apache httpclient 的示例:http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/PostSOAP.java
Look here for an example with apache httpclient: http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/PostSOAP.java
这篇关于使用 org.apache.http 发送带有 SOAP 操作的 HTTP Post 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!