http协议

请求方式:get 与post

侧重于获取资源还是发送数据、URL安全及数据量限制、字符集

 post方式请求

get方式获取jsp \js\css\img、png

POST 方式获取数据

HTTP长连接是指在一个TCP长连接中发送和接收多个HTTP报文。
相应地,短连接则指发送一个HTTP请求后直接关闭TCP连接。
HTTP1.0默认使用短连接,可以在客户端HTTP报文头部添加Connection:Keep-alive启用长连接请求。
HTTP1.1默认使用长连接,可以在客户端HTTP报文头部添加Connection:Closed关闭长连接请求。
HTTP1.1的请求报文和响应报文可以没有Connection:Keep-alive报头,但仍然是长连接。

HTTP客户端设置
以基于commons-httpclient-3.0.jar为例


Integer TIME_OUT = 5 * 1000;
String postUrl = http://10.1.1.12:8080/postData
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(TIME_OUT);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(TIME_OUT);
InputStream input = new ByteArrayInputStream(postData.getBytes("UTF-8"));
RequestEntity entity = new InputStreamRequestEntity(input);
PostMethod post = new PostMethod(postUrl);
// 选择HTTP协议版本HTTP1.1
post.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
// 显式关闭长连接
post.setRequestHeader("Connection", "Closed");
post.setRequestEntity(entity);
try {
    // 发送POST请求
    httpClient.executeMethod(post);
    String retCharSet = post.getResponseCharSet();
    StatusLine statusLine = post.getStatusLine();
    Integer statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        returnStr = new String(post.getResponseBodyAsString().getBytes(retCharSet), retCharSet);
        System.out.println("post响应: " + returnStr);
        System.out.println("post耗时: " + (System.currentTimeMillis() - startTime) + "(ms)");
    }
} finally {
    post.releaseConnection();
}

HTTP服务端设置
以免安装版Tomcat7为例
(1) 在conf/server.xml中设置keepAliveTimeout,表示多少毫秒后如果客户端没有新的请求则关闭当前连接。
<Connector acceptCount="1000" keepAliveTimeout="5000" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
keepAliveTimeout默认与connectionTimeout一致。

(2) 在conf/server.xml中设置maxKeepAliveRequests,表示一个长连接最多接收多少个请求,设置为1则为关闭长连接,设置为-1表示无限制。
<Connector acceptCount="1000" maxKeepAliveRequests="1" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值