欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java使用common-httpclient包实现post请求方法示例

时间:2021-06-12 08:14:56|栏目:JAVA代码|点击:

前言

项目中需要请求第三方接口,而且要求请求参数数据为json类型的。本来首先使用的是httpclient的jar包,但是因为项目中已经使用了common-httpclient的jar包,引起了冲突,所以不得不使用common-httpclient来实现。

示例代码:

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 httpClient介绍以及使用示例

栏    目:JAVA代码

下一篇:springMVC前台传数组类型,后台用list类型接收实例代码

本文标题:java使用common-httpclient包实现post请求方法示例

本文地址:http://www.codeinn.net/misctech/140396.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有