httppost
private static void Httppost() {
HttpClientBuilder newBuilder = HttpClientBuilder.create();
CloseableHttpClient newClient = newBuilder.build();
String url="";
HttpPost newHttppost=new HttpPost(url);
newHttppost.setConfig(RequestConfig.DEFAULT);
String postString="";
StringEntity entity=new StringEntity(postString,"UTF-8");
try {
newHttppost.setEntity(entity);
HttpResponse httpResponse=newClient.execute(newHttppost);
HttpEntity httpEntity=httpResponse.getEntity();
System.out.println("StatusLine: " +httpResponse.getStatusLine());
if (httpEntity != null) {
System.out.println("contentEncoding:" +
httpEntity.getContentEncoding());
System.out.println("response content:" +
EntityUtils.toString(httpEntity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
newClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
httpget
private static void HttpGet() {
HttpClientBuilder newBuilder=HttpClientBuilder.create();
CloseableHttpClient newClient=newBuilder.build();
HttpGet newHttpget=new HttpGet("http://hq.sinajs.cn/list=sh601006");
System.out.println("RequestLint: " + newHttpget.getRequestLine());
try {
HttpResponse httpResponse=newClient.execute(newHttpget);
HttpEntity httpEntity=httpResponse.getEntity();
System.out.println("StatusLine: " +httpResponse.getStatusLine());
if (httpEntity != null) {
System.out.println("contentEncoding:" +
httpEntity.getContentEncoding());
System.out.println("response content:" +
EntityUtils.toString(httpEntity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
newClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
RequestLint: GET http://hq.sinajs.cn/list=sh601006 HTTP/1.1
StatusLine: HTTP/1.1 200 OK
contentEncoding:null
response content:var hq_str_sh601006="..."
*/
传输实体
- UrlEncodedFormEntity()
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
NameValuePair pair1 = new BasicNameValuePair("supervisor",
supervisorEt.getEditableText().toString());
pairs.add(pair1);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
- StringEntity()
httpPost.setEntity(new StringEntity(postData.toString(), HTTP.UTF_8));