一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

服務器之家 - 編程語言 - Java教程 - Java發送post方法詳解

Java發送post方法詳解

2021-08-01 12:17xzj80927 Java教程

這篇文章主要介紹了Java發送post方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

總結一下java使用http發送post的方法:

1、post請求用于發送json 格式的參數:

?
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
/**
   * post請求(用于請求json格式的參數)
   *
   * @param url  地址
   * @param params json格式的參數
   * @return
   */
  public static string dopost(string url, string params) throws exception {
 
    closeablehttpclient httpclient = httpclients.createdefault();
    httppost httppost = new httppost( url );// 創建httppost
    httppost.setheader( "accept", "application/json" );
    httppost.setheader( "content-type", "application/json" );
    string charset = "utf-8";
    stringentity entity = new stringentity( params, charset );
    httppost.setentity( entity );
    closeablehttpresponse response = null;
 
    try {
 
      response = httpclient.execute( httppost );
      statusline status = response.getstatusline();
      int state = status.getstatuscode();
      if (state == httpstatus.sc_ok) {
        httpentity responseentity = response.getentity();
        string jsonstring = entityutils.tostring( responseentity );
        return jsonstring;
      } else {
        logger.error( "請求返回:" + state + "(" + url + ")" );
      }
    } finally {
      if (response != null) {
        try {
          response.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
      try {
        httpclient.close();
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
    return null;
  }

2、用于發送key-value格式的參數

?
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
/**
   * post請求(用于key-value格式的參數)
   *
   * @param url
   * @param params
   * @return
   */
  public static string dopost(string url, map params) {
 
    bufferedreader in = null;
    try {
      // 定義httpclient
      httpclient client = new defaulthttpclient();
      // 實例化http方法
      httppost request = new httppost();
      request.seturi( new uri( url ) );
 
      //設置參數
      list<namevaluepair> nvps = new arraylist<namevaluepair>();
      for (iterator iter = params.keyset().iterator(); iter.hasnext(); ) {
        string name = (string) iter.next();
        string value = string.valueof( params.get( name ) );
        nvps.add( new basicnamevaluepair( name, value ) );
 
        //system.out.println(name +"-"+value);
      }
      request.setentity( new urlencodedformentity( nvps, http.utf_8 ) );
 
      httpresponse response = client.execute( request );
      int code = response.getstatusline().getstatuscode();
      if (code == 200) {  //請求成功
        in = new bufferedreader( new inputstreamreader( response.getentity()
            .getcontent(), "utf-8" ) );
        stringbuffer sb = new stringbuffer( "" );
        string line = "";
        string nl = system.getproperty( "line.separator" );
        while ((line = in.readline()) != null) {
          sb.append( line + nl );
        }
        in.close();
 
        return sb.tostring();
      } else //
        system.out.println( "狀態碼:" + code );
        return null;
      }
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }

第三,發送get請求

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
   * get請求
   *
   * @return
   */
  public static string doget(string url) {
    try {
      httpclient client = new defaulthttpclient();
      //發送get請求
      httpget request = new httpget( url );
      httpresponse response = client.execute( request );
      /**請求發送成功,并得到響應**/
      if (response.getstatusline().getstatuscode() == httpstatus.sc_ok) {
        /**讀取服務器返回過來的json字符串數據**/
        string strresult = entityutils.tostring( response.getentity() );
        return strresult;
      }
    } catch (ioexception e) {
      e.printstacktrace();
    }
 
    return null;
  }

以上所述是小編給大家介紹的java發送post方法詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/xzj80927/article/details/89098334

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 无码国产成人777爽死在线观看 | 被黑人同学彻底征服全文小说阅读 | 国产原创精品 | 77色视频在线 | 国产人妖xxxxx免费看 | 久久全国免费久久青青小草 | 亚洲视频一区网站 | 国产精品亚洲片在线观看麻豆 | 欧美日韩1区2区 | 天天摸天天爽视频69视频 | 国产成人免费观看在线视频 | 波多野结衣女老师 | 日日操免费视频 | 日韩aaa | 四虎影在线永久免费观看 | 果冻传媒在线播放观看w | 国产精品一二三 | 成人动漫影院 | 欧美1| 国产在线看片网站 | 四虎影院地址 | 欧美日一级片 | 视频在线观看一区二区 | 日本大学生xxxxx69泡妞 | 猛男壮男受bl爽哭了高h | 欧美精品99 | 午夜神器老司机高清无码 | 被18号每天强行榨干acg | 男男同gayxxx | 福利国模私拍视频在线观看 | 国产xx肥老妇视频奂费 | 免费国产在线视频 | 好大好硬快点好爽公 | 青草热久精品视频在线观看 | 国产精品久久国产三级国电话系列 | 国产日韩精品一区二区三区 | 久久久久九九 | 99久在线 | 无码人妻99久久密AV | 女教师雪白老汉 | 免费91麻豆精品国产自产在线观看 |