HttpClient的相关例

本文提供多个HttpClient使用案例,包括GET和POST请求、HTTPS访问、解决中文乱码及文件上传等常见操作,适合初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HttpClient的相关例子

文章分类:Java编程

摘自 :http://hi.baidu.com/kinsonhe/blog/item/4a77d411ff00288a6538db96.html

1、HttpClient使用GET方式通过代理服务器读取页面的例子

Java代码 复制代码
  1. import java.io.BufferedReader;   
  2. import java.io.InputStreamReader;   
  3. import org.apache.http.HttpEntity;   
  4. import org.apache.http.HttpHost;   
  5. import org.apache.http.HttpResponse;   
  6. import org.apache.http.client.methods.HttpGet;   
  7. import org.apache.http.conn.params.ConnRoutePNames;   
  8. import org.apache.http.impl.client.DefaultHttpClient;   
  9.   
  10. /**  
  11. * HttpClient使用GET方式通过代理服务器读取页面的例子。  
  12.  
  13. * @author JAVA世纪网(java2000.net, laozizhu.com)  
  14. */  
  15. public class HttpClientGet {   
  16. public static void main(String[] args) throws Exception {   
  17. DefaultHttpClient httpclient = new DefaultHttpClient();   
  18. // 访问的目标站点,端口和协议   
  19. HttpHost targetHost = new HttpHost("www.java2000.net");   
  20. // 代理的设置   
  21. HttpHost proxy = new HttpHost("10.60.8.20"8080);   
  22. httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);   
  23. // 目标地址   
  24. HttpGet httpget = new HttpGet("/");   
  25. System.out.println("目标: " + targetHost);   
  26. System.out.println("请求: " + httpget.getRequestLine());   
  27. // 执行   
  28. HttpResponse response = httpclient.execute(targetHost, httpget);   
  29. HttpEntity entity = response.getEntity();   
  30. System.out.println("----------------------------------------");   
  31. System.out.println(response.getStatusLine());   
  32. if (entity != null) {   
  33. System.out.println("Response content length: " + entity.getContentLength());   
  34. }   
  35. // 显示结果   
  36. BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));   
  37. String line = null;   
  38. while ((line = reader.readLine()) != null) {   
  39. System.out.println(line);   
  40. }   
  41. if (entity != null) {   
  42. entity.consumeContent();   
  43. }   
  44. }   
  45. }  
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* HttpClient使用GET方式通过代理服务器读取页面的例子。
* 
* @author JAVA世纪网(java2000.net, laozizhu.com)
*/
public class HttpClientGet {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 访问的目标站点,端口和协议
HttpHost targetHost = new HttpHost("www.java2000.net");
// 代理的设置
HttpHost proxy = new HttpHost("10.60.8.20", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// 目标地址
HttpGet httpget = new HttpGet("/");
System.out.println("目标: " + targetHost);
System.out.println("请求: " + httpget.getRequestLine());
// 执行
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (entity != null) {
entity.consumeContent();
}
}
}

 2、HttpClient 4 使用POST方式提交普通表单数据的例子

Java代码 复制代码
  1. import java.io.BufferedReader;   
  2. import java.io.InputStreamReader;   
  3. import org.apache.http.HttpEntity;   
  4. import org.apache.http.HttpHost;   
  5. import org.apache.http.HttpResponse;   
  6. import org.apache.http.client.methods.HttpPost;   
  7. import org.apache.http.conn.params.ConnRoutePNames;   
  8. import org.apache.http.entity.StringEntity;   
  9. import org.apache.http.impl.client.DefaultHttpClient;   
  10.   
  11. /**  
  12. * HttpClient 4 使用POST方式提交普通表单数据的例子.  
  13.  
  14. * @author JAVA世纪网(java2000.net, laozizhu.com)  
  15. */  
  16. public class HttpClientPost {   
  17. public static void main(String[] args) throws Exception {   
  18. DefaultHttpClient httpclient = new DefaultHttpClient();   
  19. // 代理的设置   
  20. HttpHost proxy = new HttpHost("10.60.8.20"8080);   
  21. httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);   
  22. // 目标地址   
  23. HttpPost httppost = new HttpPost("http://www.java2000.net/login.jsp");   
  24. System.out.println("请求: " + httppost.getRequestLine());   
  25. // 构造最简单的字符串数据   
  26. StringEntity reqEntity = new StringEntity("username=test&password=test");   
  27. // 设置类型   
  28. reqEntity.setContentType("application/x-www-form-urlencoded");   
  29. // 设置请求的数据   
  30. httppost.setEntity(reqEntity);   
  31. // 执行   
  32. HttpResponse response = httpclient.execute(httppost);   
  33. HttpEntity entity = response.getEntity();   
  34. System.out.println("----------------------------------------");   
  35. System.out.println(response.getStatusLine());   
  36. if (entity != null) {   
  37. System.out.println("Response content length: " + entity.getContentLength());   
  38. }   
  39. // 显示结果   
  40. BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));   
  41. String line = null;   
  42. while ((line = reader.readLine()) != null) {   
  43. System.out.println(line);   
  44. }   
  45. if (entity != null) {   
  46. entity.consumeContent();   
  47. }   
  48. }   
  49. }  
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* HttpClient 4 使用POST方式提交普通表单数据的例子.
* 
* @author JAVA世纪网(java2000.net, laozizhu.com)
*/
public class HttpClientPost {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 代理的设置
HttpHost proxy = new HttpHost("10.60.8.20", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// 目标地址
HttpPost httppost = new HttpPost("http://www.java2000.net/login.jsp");
System.out.println("请求: " + httppost.getRequestLine());
// 构造最简单的字符串数据
StringEntity reqEntity = new StringEntity("username=test&password=test");
// 设置类型
reqEntity.setContentType("application/x-www-form-urlencoded");
// 设置请求的数据
httppost.setEntity(reqEntity);
// 执行
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (entity != null) {
entity.consumeContent();
}
}
}

 3、HttpClient 4.0通过代理访问Https的代码例子

Java代码 复制代码
  1. import java.io.BufferedReader;   
  2. import java.io.InputStreamReader;   
  3. import org.apache.http.HttpEntity;   
  4. import org.apache.http.HttpHost;   
  5. import org.apache.http.HttpResponse;   
  6. import org.apache.http.auth.AuthScope;   
  7. import org.apache.http.auth.UsernamePasswordCredentials;   
  8. import org.apache.http.client.methods.HttpGet;   
  9. import org.apache.http.conn.params.ConnRoutePNames;   
  10. import org.apache.http.impl.client.DefaultHttpClient;   
  11.   
  12. /**  
  13. * HttpClient 4.0通过代理访问Https的代码例子。  
  14.  
  15. * @author JAVA世纪网(java2000.net, laozizhu.com)  
  16. */  
  17. public class HttpsProxyGet {   
  18. public static void main(String[] args) throws Exception {   
  19. DefaultHttpClient httpclient = new DefaultHttpClient();   
  20. // 认证的数据   
  21. // 我这里是瞎写的,请根据实际情况填写   
  22. httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.60.8.20"8080),   
  23. new UsernamePasswordCredentials("username""password"));   
  24. // 访问的目标站点,端口和协议   
  25. HttpHost targetHost = new HttpHost("www.google.com"443"https");   
  26. // 代理的设置   
  27. HttpHost proxy = new HttpHost("10.60.8.20"8080);   
  28. httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);   
  29. // 目标地址   
  30. HttpGet httpget = new HttpGet("/adsense/login/zh_CN/?");   
  31. System.out.println("目标: " + targetHost);   
  32. System.out.println("请求: " + httpget.getRequestLine());   
  33. System.out.println("代理: " + proxy);   
  34. // 执行   
  35. HttpResponse response = httpclient.execute(targetHost, httpget);   
  36. HttpEntity entity = response.getEntity();   
  37. System.out.println("----------------------------------------");   
  38. System.out.println(response.getStatusLine());   
  39. if (entity != null) {   
  40. System.out.println("Response content length: " + entity.getContentLength());   
  41. }   
  42. // 显示结果   
  43. BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));   
  44. String line = null;   
  45. while ((line = reader.readLine()) != null) {   
  46. System.out.println(line);   
  47. }   
  48. if (entity != null) {   
  49. entity.consumeContent();   
  50. }   
  51. }   
  52. }  
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* HttpClient 4.0通过代理访问Https的代码例子。
* 
* @author JAVA世纪网(java2000.net, laozizhu.com)
*/
public class HttpsProxyGet {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 认证的数据
// 我这里是瞎写的,请根据实际情况填写
httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.60.8.20", 8080),
new UsernamePasswordCredentials("username", "password"));
// 访问的目标站点,端口和协议
HttpHost targetHost = new HttpHost("www.google.com", 443, "https");
// 代理的设置
HttpHost proxy = new HttpHost("10.60.8.20", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// 目标地址
HttpGet httpget = new HttpGet("/adsense/login/zh_CN/?");
System.out.println("目标: " + targetHost);
System.out.println("请求: " + httpget.getRequestLine());
System.out.println("代理: " + proxy);
// 执行
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (entity != null) {
entity.consumeContent();
}
}
}

 4、HttpClient读取页面的使用例子

Java代码 复制代码
  1. package com.laozizhu.apache.httpclient;   
  2.   
  3. import java.net.Socket;   
  4.   
  5. import org.apache.http.ConnectionReuseStrategy;   
  6. import org.apache.http.Header;   
  7. import org.apache.http.HttpHost;   
  8. import org.apache.http.HttpResponse;   
  9. import org.apache.http.HttpVersion;   
  10. import org.apache.http.impl.DefaultConnectionReuseStrategy;   
  11. import org.apache.http.impl.DefaultHttpClientConnection;   
  12. import org.apache.http.message.BasicHttpRequest;   
  13. import org.apache.http.params.BasicHttpParams;   
  14. import org.apache.http.params.HttpParams;   
  15. import org.apache.http.params.HttpProtocolParams;   
  16. import org.apache.http.protocol.BasicHttpContext;   
  17. import org.apache.http.protocol.BasicHttpProcessor;   
  18. import org.apache.http.protocol.ExecutionContext;   
  19. import org.apache.http.protocol.HttpContext;   
  20. import org.apache.http.protocol.HttpRequestExecutor;   
  21. import org.apache.http.protocol.RequestConnControl;   
  22. import org.apache.http.protocol.RequestContent;   
  23. import org.apache.http.protocol.RequestExpectContinue;   
  24. import org.apache.http.protocol.RequestTargetHost;   
  25. import org.apache.http.protocol.RequestUserAgent;   
  26. import org.apache.http.util.EntityUtils;   
  27.   
  28. /**  
  29. * HttpClient读取页面的使用例子  
  30. * @author 老紫竹(java2000.net)  
  31. *  
  32. */  
  33. public class HttpGet {   
  34. public static void main(String[] args) throws Exception {   
  35.   
  36.     HttpParams params = new BasicHttpParams();   
  37. // HTTP 协议的版本,1.1/1.0/0.9   
  38. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);   
  39. // 字符集   
  40. HttpProtocolParams.setContentCharset(params, "UTF-8");   
  41. // 伪装的浏览器类型   
  42. // IE7 是    
  43. // Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)   
  44. //   
  45. // Firefox3.03   
  46. // Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3   
  47. //   
  48. HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");   
  49. HttpProtocolParams.setUseExpectContinue(params, true);   
  50.   
  51.     BasicHttpProcessor httpproc = new BasicHttpProcessor();   
  52.   
  53.     httpproc.addInterceptor(new RequestContent());   
  54. httpproc.addInterceptor(new RequestTargetHost());   
  55.   
  56.     httpproc.addInterceptor(new RequestConnControl());   
  57. httpproc.addInterceptor(new RequestUserAgent());   
  58. httpproc.addInterceptor(new RequestExpectContinue());   
  59.   
  60.     HttpRequestExecutor httpexecutor = new HttpRequestExecutor();   
  61.   
  62.     HttpContext context = new BasicHttpContext(null);   
  63. HttpHost host = new HttpHost("www.java2000.net"80);   
  64.   
  65.     DefaultHttpClientConnection conn = new DefaultHttpClientConnection();   
  66. ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();   
  67.   
  68.     context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);   
  69. context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);   
  70.   
  71.     try {   
  72.   
  73.       String[] targets = { "/""/help.jsp" };   
  74.   
  75.       for (int i = 0; i < targets.length; i++) {   
  76. if (!conn.isOpen()) {   
  77. Socket socket = new Socket(host.getHostName(), host.getPort());   
  78. conn.bind(socket, params);   
  79. }   
  80. BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);   
  81. System.out.println(">> Request URI: " + request.getRequestLine().getUri());   
  82.   
  83.         context.setAttribute(ExecutionContext.HTTP_REQUEST, request);   
  84. request.setParams(params);   
  85. httpexecutor.preProcess(request, httpproc, context);   
  86. HttpResponse response = httpexecutor.execute(request, conn, context);   
  87. response.setParams(params);   
  88. httpexecutor.postProcess(response, httpproc, context);   
  89.   
  90.         // 返回码   
  91. System.out.println("<< Response: " + response.getStatusLine());   
  92. // 返回的文件头信息   
  93. Header[] hs = response.getAllHeaders();   
  94. for (Header h : hs) {   
  95. System.out.println(h.getName() + ":" + h.getValue());   
  96. }   
  97. // 输出主体信息   
  98. System.out.println(EntityUtils.toString(response.getEntity()));   
  99. System.out.println("==============");   
  100. if (!connStrategy.keepAlive(response, context)) {   
  101. conn.close();   
  102. else {   
  103. System.out.println("Connection kept alive...");   
  104. }   
  105. }   
  106. finally {   
  107. conn.close();   
  108. }   
  109. }   
  110. }  
package com.laozizhu.apache.httpclient;

import java.net.Socket;

import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;

/**
* HttpClient读取页面的使用例子
* @author 老紫竹(java2000.net)
*
*/
public class HttpGet {
public static void main(String[] args) throws Exception {

    HttpParams params = new BasicHttpParams();
// HTTP 协议的版本,1.1/1.0/0.9
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// 字符集
HttpProtocolParams.setContentCharset(params, "UTF-8");
// 伪装的浏览器类型
// IE7 是 
// Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)
//
// Firefox3.03
// Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
//
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);

    BasicHttpProcessor httpproc = new BasicHttpProcessor();

    httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());

    httpproc.addInterceptor(new RequestConnControl());
httpproc.addInterceptor(new RequestUserAgent());
httpproc.addInterceptor(new RequestExpectContinue());

    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

    HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("www.java2000.net", 80);

    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

    try {

      String[] targets = { "/", "/help.jsp" };

      for (int i = 0; i < targets.length; i++) {
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
System.out.println(">> Request URI: " + request.getRequestLine().getUri());

        context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);

        // 返回码
System.out.println("<< Response: " + response.getStatusLine());
// 返回的文件头信息
Header[] hs = response.getAllHeaders();
for (Header h : hs) {
System.out.println(h.getName() + ":" + h.getValue());
}
// 输出主体信息
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("==============");
if (!connStrategy.keepAlive(response, context)) {
conn.close();
} else {
System.out.println("Connection kept alive...");
}
}
} finally {
conn.close();
}
}
}

 5、httpclient中文乱码解决

Java代码 复制代码
  1. httpclient默认使用ISO-8859-1读取http响应的内容,如果内容中包含汉字的话就得动用丑陋的new String(str.getBytes("ISO-8859-1"),"GBK");语句了。    
  2.   
  3. 解决办法就是使用以下配置。   
  4.   
  5. private static final String CONTENT_CHARSET = "GBK";// httpclient读取内容时使用的字符集   
  6.   
  7. HttpClient client = new HttpClient();   
  8. client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);  
httpclient默认使用ISO-8859-1读取http响应的内容,如果内容中包含汉字的话就得动用丑陋的new String(str.getBytes("ISO-8859-1"),"GBK");语句了。 

解决办法就是使用以下配置。

private static final String CONTENT_CHARSET = "GBK";// httpclient读取内容时使用的字符集

HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);

 6、HttpClient 4处理文件上传的例子(MultipartEntity)

Java代码 复制代码
  1. import java.io.File;   
  2. import org.apache.http.HttpEntity;   
  3. import org.apache.http.HttpResponse;   
  4. import org.apache.http.client.HttpClient;   
  5. import org.apache.http.client.methods.HttpPost;   
  6. import org.apache.http.entity.mime.MultipartEntity;   
  7. import org.apache.http.entity.mime.content.FileBody;   
  8. import org.apache.http.entity.mime.content.StringBody;   
  9. import org.apache.http.impl.client.DefaultHttpClient;   
  10.   
  11. /**  
  12. * HttpClient 4处理文件上传的例子(MultipartEntity).<br>  
  13. * 需要 James Mime4j 0.5的版本,0.6的不行。  
  14.  
  15. * @author JAVA世纪网(java2000.net, laozizhu.com)  
  16. */  
  17. public class HttpClientMultipartFormPost {   
  18. public static void main(String[] args) throws Exception {   
  19. HttpClient httpclient = new DefaultHttpClient();   
  20. HttpPost httppost = new HttpPost("http://localhost");   
  21. // 一个本地的文件   
  22. FileBody bin = new FileBody(new File("d:/BIMG1181.JPG"));   
  23. // 一个字符串   
  24. StringBody comment = new StringBody("A binary file of some kind");   
  25. // 多部分的实体   
  26. MultipartEntity reqEntity = new MultipartEntity();   
  27. // 增加   
  28. reqEntity.addPart("bin", bin);   
  29. reqEntity.addPart("comment", comment);   
  30. // 设置   
  31. httppost.setEntity(reqEntity);   
  32. System.out.println("执行: " + httppost.getRequestLine());   
  33. HttpResponse response = httpclient.execute(httppost);   
  34. HttpEntity resEntity = response.getEntity();   
  35. System.out.println("----------------------------------------");   
  36. System.out.println(response.getStatusLine());   
  37. if (resEntity != null) {   
  38. System.out.println("返回长度: " + resEntity.getContentLength());   
  39. }   
  40. if (resEntity != null) {   
  41. resEntity.consumeContent();   
  42. }   
  43. }   
  44. }  

 

httpclient GetMethod 传参数 支持中文

 

    private static HttpMethod getGetMethod()throws Exception{
     String propertyValue1 = "北京";
     String propertyValue2= "大厦";
     String parameterValue1 = URLEncoder.encode(propertyValue1,"gbk");
     String parameterValue2 = URLEncoder.encode(propertyValue2,"gbk");
    
       return new GetMethod("/jquery/index.action?username="+parameterValue1+"&password="+parameterValue2);
    }


    String result = new String(method.getResponseBodyAsString().getBytes("iso-8859-1"));
     System.out.println(result);   

这样一处理 就解决了问题,研究了半天才搞定,很简单的。

 

 

 

个人签名

-------------------------------------

 

图盾 淘宝保护 保护图片 图片防盗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值