发送HTTP POST请求,使用SOAP org.apache.http行动 [英] Sending HTTP Post request with SOAP action using org.apache.http

查看:291
本文介绍了发送HTTP POST请求,使用SOAP org.apache.http行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写与SOAP动作的硬codeD HTTP POST请求,使用org.apache.http API。
我的问题是我没有找到一个方法来添加一个请求主体(在我的情况 - SOAP动作)。
我会很高兴的一些指导。

 进口java.net.URI中;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.entity.StringEntity;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.impl.client.RequestWrapper;
进口org.apache.http.protocol.HTTP;公共类HTT prequest
{
    @燮pressWarnings(未使用)
    公共HTT prequest()
    {
        尝试{
            HttpClient的HttpClient的=新DefaultHttpClient();
            绳体=DataDataData;
            字符串bodyLength =新的整数(body.length())的toString()。
            的System.out.println(bodyLength);
// StringEntity stringEntity =新StringEntity(体);            URI URI =新的URI(SOMEURL参数1 = 1234&放大器;参数2 = ABCD);
            HttpPost httpPost =新HttpPost(URI);
            httpPost.addHeader(测试,Test_Value);// httpPost.setEntity(stringEntity);            StringEntity实体=新StringEntity(身体,文/ XML,HTTP.DEFAULT_CONTENT_CHARSET);
            httpPost.setEntity(实体);            RequestWrapper此类RequestWrapper此类=新RequestWrapper此类(httpPost);
            requestWrapper.setMethod(POST);
            requestWrapper.setHeader(LuckyNumber,77);
            requestWrapper.removeHeaders(主机);
            requestWrapper.setHeader(主机,GOD_IS_A_DJ);
// requestWrapper.setHeader(内容长度,bodyLength);
            HTT presponse响应= httpclient.execute(RequestWrapper此类);
        }赶上(例外五){
            e.printStackTrace();
        }
    }
}


解决方案

将SOAPAction必须作为一个HTTP头参数传递 - 使用时,它不是HTTP体/负载的一部分

在这里寻找与Apache HttpClient的一个例子:的http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/PostSOAP.java

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();
        }
    }
}

解决方案

The soapAction must passed as a http-header parameter - when used, it's not part of the http-body/payload.

Look here for an example with apache httpclient: http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/PostSOAP.java

这篇关于发送HTTP POST请求,使用SOAP org.apache.http行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆