在类对象

时间:2016-09-27 22:51:33

标签: java apache http-headers httprequest httpclient

我使用DefaultHttpClientConnection发送http post请求。是否可以重用http连接?例如,实例化并设置连接并将套接字绑定到它,然后将其分配给类中的静态字段。然后,对于发送http帖子的每个函数调用,我将重用我之前设置的相同DefaultHttpClientConnection。我试图以这种方式重用它,但我一直得到一个HTTPException,说我有无效的协议。如果我实例化一个新的DefaultHttpClientConnection并在每次发送帖子时设置它,代码都可以工作,但每当我尝试提高效率并重复使用它时,我就会得到该异常。

这是Java。

  /*
   * These settings shall speed up the sending of the http request...
   * */
  BasicHttpParams params = new BasicHttpParams();
  //Use HTTP 1.1 so we open a TCP connection per request sent
  params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
  //Turn off Nagle's Algorithm to speed things up
  HttpConnectionParams.setTcpNoDelay(params, true);
  params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
  params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false);
  params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024);
  client.setParams(params);
  DefaultConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

  HttpResponse resp = null;
  if (conn == null)
  {
    executor = new HttpRequestExecutor();
    context = new BasicHttpContext();
    httpproc = new BasicHttpProcessor();
    /*Set http connection processors, mostly default settings
     * What these protocols do, can be easily found online ;) */
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    // Recommended protocol interceptors
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());
  }

  conn = new DefaultHttpClientConnection();
  if (!conn.isOpen())
  {
    //Opening the socket
    try
    {
      socket = new Socket(request.getURI().getHost(), request.getURI().getPort());
      try
      {
        //Bind socket to the connection
        TTLog.log("connection binding to socket...");
        conn.bind(socket, client.getParams());
      }
      catch (IOException e1)
      {
        TTLog.log("Failed to bind the socket to the HTTPConnection...");
        e1.printStackTrace();
      }
    }
    catch (UnknownHostException e1)
    {
      TTLog.log("The host name is invalid... Hostname: " + request.getURI().getHost());
      e1.printStackTrace();
    }
    catch (IOException e1)
    {
      TTLog.log("I/O Error when creating socket...");
      e1.printStackTrace();
    }
  }

以下是例外:

  

org.apache.http.ProtocolException:不是有效的协议版本:??)»L   在   org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:181)   在   org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:185)   在   org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:284)   在   org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:126)

1 个答案:

答案 0 :(得分:0)

HTTP是无状态且无连接的协议。这意味着服务器和客户端只是满足当前请求,然后会话处理,他们忘记了彼此。如果要保持连接会话,则需要使用Websockets。