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

发布时间:2021-05-11 16:32:43 作者:Leah
来源:亿速云 阅读:279

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

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();
  }
 }
}

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

推荐阅读:
  1. 在PHP中怎么实现GET和POST请求的方法
  2. 如何使用java中的包

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java post

上一篇:Druid怎么在SpringBoot中使用

下一篇:如何在Android中使用ViewDragHelper实现图片下拽返回

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》