新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么在java中使用common-httpclient包实现post请求

怎么在java中使用common-httpclient包实现post请求?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联公司专注于企业网络营销推广、网站重做改版、海原网站定制设计、自适应品牌网站建设、H5响应式网站商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为海原等各大城市提供网站开发制作服务。

Java的特点有哪些

Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HTTPUtils {

 private static Logger logger = LoggerFactory.getLogger(HTTPUtils.class);

 /**
  * post请求
  * @param url
  * @param json
  * @return
  */
 public static String postJosnContent(String url, String Json) throws Exception {
//  HttpPost method = new HttpPost(url); 
//  DefaultHttpClient httpClient = new DefaultHttpClient(); 
//  String ret = null;
//  try {
//   StringEntity entity = new StringEntity(Json,"UTF-8");//解决中文乱码问题  
//    entity.setContentEncoding("UTF-8"); 
//    entity.setContentType("application/json");
//    method.setEntity(entity); 
//    HttpResponse result = httpClient.execute(method); 
//    ret = EntityUtils.toString(result.getEntity()); 
//  } catch (Exception e) {
//   throw e;
//  } finally {
//   method.releaseConnection();
//  }
//  return ret;
  logger.error("请求接口参数:" + Json);
  PostMethod method = new PostMethod(url);
  HttpClient httpClient = new HttpClient();
  try {
   RequestEntity entity = new StringRequestEntity(Json,"application/json","UTF-8");
   method.setRequestEntity(entity);
   httpClient.executeMethod(method);
   logger.error("请求接口路径url:" + method.getURI().toString());
   InputStream in = method.getResponseBodyAsStream();
   //下面将stream转换为String
   StringBuffer sb = new StringBuffer();
   InputStreamReader isr = new InputStreamReader(in, "UTF-8");
   char[] b = new char[4096];
   for(int n; (n = isr.read(b)) != -1;) {
    sb.append(new String(b, 0, n));
   }
   String returnStr = sb.toString();
   return returnStr;
  } catch (Exception e) {
   e.printStackTrace();
   throw e;
  } finally {
   method.releaseConnection();
  }
 }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


本文题目:怎么在java中使用common-httpclient包实现post请求
本文URL:http://www.wjwzjz.com/article/jcigje.html
在线咨询
服务热线
服务热线:028-86922220
TOP