首页 > 分享 > HttpURLConnection发出的post请求

HttpURLConnection发出的post请求

HttpURLConnection

今天写了个http客户端,代码很简单,网上的资料也一大堆。我从网上copy一份HttpURLConnection就开始测试,测试的时候发现每次都返回给我500。

String urlString = "***";

String params = "{****}";

URL url;

try {

url = new URL(urlString);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0");

conn.setRequestMethod("POST");

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setConnectTimeout(1000 * 5);

conn.getOutputStream().write(params.getBytes("utf8"));

conn.getOutputStream().flush();

conn.getOutputStream().close();

byte[] buffer = new byte[1024];

StringBuffer sb = new StringBuffer();

InputStream in = conn.getInputStream();

int httpCode = conn.getResponseCode();

System.out.println(in.available());

while(in.read(buffer,0,1024) != -1) {

sb.append(new String(buffer));

}

System.out.println("sb:" + sb.toString());

in.close();

System.out.println(httpCode);

}catch(Exception e) {

e.printStackTrace();

}

这高手来说这不是什么难题。

了解此问题,需要了解Http协议中的Header。

 

HttpClient

通过Apache开源的Httpclient测试,顺利返回200。因为我一直很抵触开源的东西(尤其是ssh),能自己写的就尽量自己写,所以我还得花点时间在HttpURLConnection上。

HttpClient测试代码:

String result = null;

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

HttpPost httpPost = new HttpPost(url);

StringEntity stringEntity = new StringEntity(params,ContentType.create("plain/text", Consts.UTF_8));

httpPost.setEntity(stringEntity);

System.out.println(httpPost.getRequestLine());

try {

HttpResponse httpResponse = closeableHttpClient.execute(httpPost);

HttpEntity entity = httpResponse.getEntity();

System.out.println("status:" + httpResponse.getStatusLine());

if (entity != null) {

System.out.println("contentEncoding:" + entity.getContentEncoding());

System.out.println("response content:"+ EntityUtils.toString(entity));

}

} catch (IOException e) {

throw e;

} finally {

try {

closeableHttpClient.close();

} catch (IOException e) {

throw e;

}

}


Wireshark

抓包能然事情变得一目了然,对比后发现,Content-Type不同。

Content-Type: plain/text; charset=UTF-8

在HttpURLConnection里加上Content-Type,就能顺利返回200。

conn.setRequestProperty("Content-Type", "plain/text; charset=UTF-8");

Content-Type的定义如下:

Content-Type,内容类型,一般是指网页中存在的Content-Type,用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式、什么编码读取这个文件。

Content-Type 字段是非常重要的属性,在调用前一定要知道对方是什么编码。

HttpURLConnection何时发送请求?

除了Content-Type的问题,在抓包过程中发现了其他知识点。即HttpURLConnection何时发送请求?

查看了HttpURLConnection源码,一头污水,还是配合Wireshark查看更清楚一些。

eclipse里设置好了断点,开始debug,另一边用wireshark抓包,结果是这样的。

当执行完output.flush的时候,HttpURLConnection才开始与对方建立三次握手。

wireshark中

当执行conn.getInputStream()的时候才开始发送数据

eclipse里

wireshark中

总结

1. 写http客户端一定要先约定好编码格式。

2. 在HttpURLConnection post时,flush后建立连接,getInputstream时提交数据。

写的不好,请批评指正。

相关知识

requests 发送 post请求的三种方式
Java项目如何调用支付接口
http请求响应模型
android之底层网络库
「IP地址/域名查询」我要查
WebService通过Get方式调用第三方查询电话号码归属地
ios开发网络请求Domain=com.alamofire.error.serialization.response Code=
网页Request Headers请求头和Response Headers响应头
非法请求
请求原谅送什么花合适

网址: HttpURLConnection发出的post请求 https://m.huajiangbk.com/newsview1998538.html

所属分类:花卉
上一篇: 辰代表的方位
下一篇: 冰城公安:扎根社区 让平安之花绽