溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在java中使用common-httpclient包實現(xiàn)post請求

發(fā)布時間:2021-05-11 16:32:43 來源:億速云 閱讀:282 作者:Leah 欄目:編程語言

怎么在java中使用common-httpclient包實現(xiàn)post請求?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

Java的特點有哪些

Java的特點有哪些 1.Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,實現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向?qū)ο?、分布式、安全性、平臺獨立與可移植性、動態(tài)性等特點。 3.使用Java可以編寫桌面應用程序、Web應用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應用程序等。

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("請求接口參數(shù):" + 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轉(zhuǎn)換為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();
  }
 }
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI