java – 使用org.apache.http发送带有SOAP操作的HTTP Post请求

前端之家收集整理的这篇文章主要介绍了java – 使用org.apache.http发送带有SOAP操作的HTTP Post请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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();
        }
    }
}

解决方法

soapAction必须作为http-header参数传递 – 当使用它时,它不是http-body / payload的一部分.

看看这里一个例子与apache httpclient:http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/PostSOAP.java

原文链接:https://www.f2er.com/java/126782.html

猜你在找的Java相关文章