今天写了个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。
通过Apache开源的Httpclient测试,顺利返回200。因为我一直很抵触开源的东西(尤其是ssh),能自己写的就尽量自己写,所以我还得花点时间在HttpURLConnection上。
HttpClient测试代码:
String result = null;
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
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 {
// 执行get请求
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;
}
}
抓包能然事情变得一目了然,对比后发现,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 字段是非常重要的属性,在调用前一定要知道对方是什么编码。
除了Content-Type的问题,在抓包过程中发现了其他知识点。即HttpURLConnection何时发送请求?
查看了HttpURLConnection源码,一头污水,还是配合Wireshark查看更清楚一些。
eclipse里设置好了断点,开始debug,另一边用wireshark抓包,结果是这样的。
当执行完output.flush的时候,HttpURLConnection才开始与对方建立三次握手。
wireshark中
当执行conn.getInputStream()的时候才开始发送数据
eclipse里
wireshark中
1. 写http客户端一定要先约定好编码格式。
2. 在HttpURLConnection post时,flush后建立连接,getInputstream时提交数据。
写的不好,请批评指正。