使用Java HttpCore发送请求时,HttpRequest实体(正文)为空

时间:2012-03-25 16:52:17

标签: java http post httprequest apache-httpcomponents

已经尝试将一般POST消息发送到服务器的小时数。 我正在使用HTTP核心库。

所有标题似乎都是正确编写的,但不知何故,邮件的正文(实体)正在被删除而不会被发送到服务器。 调用“process”命令或相同的“preProcess”后会出现问题。 注意:这几乎与官方Apache示例相同。 感谢任何建议。

import java.net.*;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.ImmutableHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;

public class HTTPCommunicator
{
    public static String TOKEN = null;

    public static void main(String[] args) throws Exception
    {           
        BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/api");

        HttpHost host = new HttpHost("localhost", 80);

        HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
        HttpContext context = new BasicHttpContext(null);
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

        request.addHeader("Host", "localhost");
        request.addHeader("Accept", "application/json,application/xml");
        request.addHeader("Accept-Encoding", "gzip, deflate");

        HttpParams params = new SyncBasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setUserAgent(params, "MyClient");
        HttpProtocolParams.setUseExpectContinue(params, true);

        request.setParams(params);      

        String sRequest = "userName=yourusername&password=yourpassword";
        request.setEntity(new StringEntity(sRequest));

        HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
                // Required protocol interceptors
        new RequestContent(), new RequestTargetHost(),
                // Recommended protocol interceptors
        new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() });

        httpproc.process(request, context);

        //httpexecutor.preProcess(request, httpproc, context);

        //Sending request
        Socket socket = new Socket(host.getHostName(), host.getPort());
        conn.bind(socket, params);
        HttpResponse response = httpexecutor.execute(request, conn, context);

        System.out.println(EntityUtils.toString(response.getEntity()));
    }

1 个答案:

答案 0 :(得分:0)

我认为问题是由服务器未正确处理expect-continue握手引起的。从协议处理链中删除RequestExpectContinue协议拦截器,看看是否有任何区别。