国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java使用common-httpclient包實現post請求方法示例

java使用common-httpclient包實現post請求方法示例

2021-05-25 12:09噬月幽狼 Java教程

這篇文章主要給大家介紹了關于java使用common-httpclient包實現post請求的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

項目中需要請求第三方接口,而且要求請求參數數據為json類型的。本來首先使用的是httpclient的jar包,但是因為項目中已經使用了common-httpclient的jar包,引起了沖突,所以不得不使用common-httpclient來實現。

示例代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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();
  }
 }
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://blog.csdn.net/u010398838/article/details/80708632

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 在线观看免费黄色小视频 | 中日韩午夜理伦电影免费 | www日韩 | 欧美国产在线视频 | 亚洲视频中文字幕 | 久久久国产一区二区三区 | 欧美大片免费高清观看 | 久久男人 | 国产精品爱久久久久久久 | 日韩三级网址 | 自拍偷拍精品 | 亚洲精品一区二区三区在线 | 国产中文字幕在线观看 | 黄色片网站视频 | 视频一区二区三区中文字幕 | 一区二区三区免费 | 欧美久久视频 | 日韩在线视频一区 | 国产精品久久久久aaaa九色 | 91激情视频 | 久久久高清| 黄桃av| 成人免费观看视频大全 | 国产精品久久久久久久9999 | 精品亚洲永久免费精品 | 精品福利在线 | 成人午夜天堂 | 一 级 黄 色 片免费网站 | 成人久久久久久久久 | 亚洲wu码 | www.91福利 | 一区二区三区久久 | 欧美成人激情视频 | 免费一区二区三区四区 | 99re在线| 久久成人人人人精品欧 | 日韩精品专区 | 午夜一级片 | 亚洲国产精品久久 | 91福利视频导航 | 人人叉人人 |