1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢(xún)
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
      怎么在Java中發(fā)送post

      本篇文章給大家分享的是有關(guān)怎么在Java中發(fā)送post,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

      成都創(chuàng)新互聯(lián)是一家專(zhuān)注于成都做網(wǎng)站、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),保亭黎族網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:保亭黎族等地區(qū)。保亭黎族做網(wǎng)站價(jià)格咨詢(xún):13518219792

      1、post請(qǐng)求用于發(fā)送json 格式的參數(shù):

      /**
         * post請(qǐng)求(用于請(qǐng)求json格式的參數(shù))
         *
         * @param url  地址
         * @param params json格式的參數(shù)
         * @return
         */
        public static String doPost(String url, String params) throws Exception {
       
          CloseableHttpClient httpclient = HttpClients.createDefault();
          HttpPost httpPost = new HttpPost( url );// 創(chuàng)建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( "請(qǐng)求返回:" + 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、用于發(fā)送key-value格式的參數(shù)

      /**
         * post請(qǐng)求(用于key-value格式的參數(shù))
         *
         * @param url
         * @param params
         * @return
         */
        public static String doPost(String url, Map params) {
       
          BufferedReader in = null;
          try {
            // 定義HttpClient
            HttpClient client = new DefaultHttpClient();
            // 實(shí)例化HTTP方法
            HttpPost request = new HttpPost();
            request.setURI( new URI( url ) );
       
            //設(shè)置參數(shù)
            List nvps = new ArrayList();
            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) {  //請(qǐng)求成功
              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( "狀態(tài)碼:" + code );
              return null;
            }
          } catch (Exception e) {
            e.printStackTrace();
            return null;
          }
        }

      第三,發(fā)送get請(qǐng)求

      /**
         * get請(qǐng)求
         *
         * @return
         */
        public static String doGet(String url) {
          try {
            HttpClient client = new DefaultHttpClient();
            //發(fā)送get請(qǐng)求
            HttpGet request = new HttpGet( url );
            HttpResponse response = client.execute( request );
            /**請(qǐng)求發(fā)送成功,并得到響應(yīng)**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
              /**讀取服務(wù)器返回過(guò)來(lái)的json字符串?dāng)?shù)據(jù)**/
              String strResult = EntityUtils.toString( response.getEntity() );
              return strResult;
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
       
          return null;
        }

      Java的特點(diǎn)有哪些

      Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚摚试S程序員以?xún)?yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο蟆⒎植际健踩浴⑵脚_(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫(xiě)桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

      以上就是怎么在Java中發(fā)送post,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


      文章名稱(chēng):怎么在Java中發(fā)送post
      當(dāng)前鏈接:http://www.ef60e0e.cn/article/jejcdd.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        游戏| 思南县| 南投市| 昌江| 绵竹市| 垣曲县| 瑞安市| 博兴县| 介休市| 上蔡县| 韶山市| 湛江市| 高雄县| 容城县| 永靖县| 区。| 于田县| 新化县| 孙吴县| 浙江省| 岗巴县| 奇台县| 大兴区| 汉源县| 海伦市| 宿迁市| 渝中区| 锡林浩特市| 兴隆县| 闸北区| 长泰县| 红安县| 轮台县| 郸城县| 上虞市| 进贤县| 彰化县| 扎鲁特旗| 剑阁县| 西林县| 田东县|