热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

httpclient的几种post参数格式

1、json格式HttpClientContextcontextHttpClientContext.create();httpPost.setU

1、json格式

HttpClientContext cOntext= HttpClientContext.create();

httpPost.setURI(java.net.URI.create(url));

if(null != headers) {
for (String name : headers.keySet()) {
httpPost.setHeader(name, headers.get(name));
}
}

StringEntity entity1 = new StringEntity(json, HTTP.UTF_8);
httpPost.setEntity(entity1);
entity1.setContentType("application/json");


System.out.println("Executing request " + httpPost.getRequestLine() + " to " + " via " + proxy);

CloseableHttpResponse respOnse= httpClient.execute(httpPost,context);
response.setLocale(Locale.CHINESE);

long end = System.currentTimeMillis();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
entity = response.getEntity() ;

String html = EntityUtils.toString(entity, charSet);
response.close();

entity.getContent().close();
}

2、urlencoded格式

HttpClientContext cOntext= HttpClientContext.create();

httpPost.setURI(java.net.URI.create(url));

if(null != headers) {
for (String name : headers.keySet()) {
httpPost.setHeader(name, headers.get(name));
}
}


StringEntity entity1 = new StringEntity(postDataEncode, HTTP.UTF_8);

entity1.setContentType("application/x-www-form-urlencoded");

httpPost.setEntity(entity1);

System.out.println("Executing request " + httpPost.getRequestLine() + " to " + " via " + proxy);

CloseableHttpResponse respOnse= httpClient.execute(httpPost,context);
response.setLocale(Locale.CHINESE);

long end = System.currentTimeMillis();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
entity = response.getEntity() ;

String html = EntityUtils.toString(entity, charSet);
response.close();

entity.getContent().close();
}

3、key-value格式

HttpClientContext cOntext= HttpClientContext.create();

httpPost = new HttpPost(url);

if(null != headers) {
for (String name : headers.keySet()) {
httpPost.setHeader(name, headers.get(name));
}
}

if(null != params) {
List nvps = new ArrayList();
for (String name : params.keySet()) {
nvps.add(new BasicNameValuePair(name, params.get(name)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
}


System.out.println("Executing request " + httpPost.getRequestLine() + " to " + " via " + proxy);

CloseableHttpResponse respOnse= httpClient.execute(httpPost,context);
response.setLocale(Locale.CHINESE);

String redirectUrl = "";
List redirectUrls = context.getRedirectLocations();
if(redirectUrls!=null){
redirectUrl = redirectUrls.get(0).toString();
System.out.println("重定向:" + redirectUrl);
}

long end = System.currentTimeMillis();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");
// System.out.println(EntityUtils.toString(response.getEntity()));

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
entity = response.getEntity() ;

String html = EntityUtils.toString(entity, charSet);
response.close();
// httpGet.releaseConnection();

entity.getContent().close();

}

4、MultipartForm

 CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost("http://localhost:8080" +
"/servlets-examples/servlet/RequestInfoExample");

FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("bin", bin)
.addPart("comment", comment)
.build();


httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());
CloseableHttpResponse respOnse= httpclient.execute(httppost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}

5、XML格式

 File input = new File(“test.xml”);
PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);

// 设置请求的内容直接从文件中读取
post.setRequestBody( new FileInputStream(input));
if (input.length() .MAX_VALUE)
post.setRequestContentLength(input.length());
else
post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

// 指定请求内容的类型
post.setRequestHeader( "Content-type" , "text/xml; charset=GBK" );
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
System.out.println( "Response status code: " + result);
System.out.println( "Response body: " );
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();

推荐阅读
author-avatar
810526猪肝
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有