亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中Https發(fā)送POST請求[親測可用]

 更新時(shí)間:2021年05月10日 16:21:56   作者:不是植物  
這篇文章主要介紹了Java中Https發(fā)送POST請求[親測可用],本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、直接建一個(gè)工具類放入即可

/**
 * 發(fā)送https請求共用體 
 */
public  static JSONObject  sendPost(String url,String parame,Map<String,Object> pmap) throws IOException, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException{
    // 請求結(jié)果
    JSONObject json = new JSONObject();
    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    URL realUrl;
    HttpsURLConnection conn;
    String method = "POST";
    //查詢地址
    String queryString = url;
    //請求參數(shù)獲取
    String postpar = "";
    //字符串請求參數(shù)
    if(parame!=null){
        postpar = parame;
    }
    // map格式的請求參數(shù)
    if(pmap!=null){
        StringBuffer mstr = new StringBuffer();
        for(String str:pmap.keySet()){
            String val = (String) pmap.get(str);
            try {
                val=URLEncoder.encode(val,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
                mstr.append(str+"="+val+"&");
        }
        // 最終參數(shù)
        postpar = mstr.toString(); 
        int lasts=postpar.lastIndexOf("&");
        postpar=postpar.substring(0, lasts);
    }
    if(method.toUpperCase().equals("GET")){
        queryString+="?"+postpar;
    }
    SSLSocketFactory  ssf= HttpsClientUtils.getSSFactory();
    try {
        realUrl= new URL(queryString);
        conn = (HttpsURLConnection)realUrl.openConnection();
        conn.setSSLSocketFactory(ssf);
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        if(method.toUpperCase().equals("POST")){
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            out = new PrintWriter(conn.getOutputStream());
            out.print(postpar);
            out.flush();
        }else{
            conn.connect();
        }
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream(),"utf-8"));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        json = JSONObject.fromObject(result);
    }finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return json;
}

2、可能需要的包

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

到此這篇關(guān)于Java中Https發(fā)送POST請求[親測可用]的文章就介紹到這了,更多相關(guān)https發(fā)送post請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 線程池ThreadPoolExecutor并行處理實(shí)現(xiàn)代碼

    線程池ThreadPoolExecutor并行處理實(shí)現(xiàn)代碼

    這篇文章主要介紹了線程池ThreadPoolExecutor并行處理實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 淺談SpringMVC中的session用法及細(xì)節(jié)記錄

    淺談SpringMVC中的session用法及細(xì)節(jié)記錄

    下面小編就為大家?guī)硪黄獪\談SpringMVC中的session用法及細(xì)節(jié)記錄。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java實(shí)現(xiàn)商品管理系統(tǒng)代碼實(shí)例講解

    Java實(shí)現(xiàn)商品管理系統(tǒng)代碼實(shí)例講解

    這篇文章主要介紹了Java實(shí)現(xiàn)商品管理系統(tǒng)代碼實(shí)例講解,文中代碼實(shí)例講解的很清楚,有需要的同學(xué)可以借鑒參考下
    2021-02-02
  • Java 線程池原理深入分析

    Java 線程池原理深入分析

    這篇文章主要介紹了Java 線程池原理深入分析的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Java 添加、刪除、格式化Word中的圖片步驟詳解( 基于Spire.Cloud.SDK for Java )

    Java 添加、刪除、格式化Word中的圖片步驟詳解( 基于Spire.Cloud.SDK for Java )

    這篇文章主要介紹了Java 添加、刪除、格式化Word中的圖片( 基于Spire.Cloud.SDK for Java ),本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 如何徹底刪除SVN中的文件和文件夾(附恢復(fù)方法)

    如何徹底刪除SVN中的文件和文件夾(附恢復(fù)方法)

    在SVN中如果刪除某個(gè)文件或文件夾也可以在歷史記錄中進(jìn)行找回,有的時(shí)候需要徹底刪除某些文件,即不希望通過歷史記錄進(jìn)行恢復(fù),需要在服務(wù)器上對SVN的數(shù)據(jù)進(jìn)行重新整理
    2014-08-08
  • 深入了解JAVA HASHMAP的死循環(huán)

    深入了解JAVA HASHMAP的死循環(huán)

    HASHMAP基于哈希表的 Map 接口的實(shí)現(xiàn)。此實(shí)現(xiàn)提供所有可選的映射操作,并允許使用 null 值和 null 鍵。(除了非同步和允許使用 null 之外,HashMap 類與 Hashtable 大致相同。)下面小編來帶大家詳細(xì)了解下吧
    2019-06-06
  • Java線程通信之wait-notify通信方式詳解

    Java線程通信之wait-notify通信方式詳解

    這篇文章主要為大家詳細(xì)介紹了Java線程通信之wait-notify通信方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • SpringBoot2.0實(shí)現(xiàn)多圖片上傳加回顯

    SpringBoot2.0實(shí)現(xiàn)多圖片上傳加回顯

    這兩天公司有需求讓做一個(gè)商戶注冊的后臺(tái)功能,其中需要商戶上傳多張圖片并回顯,本文就使用SpringBoot2.0實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-07-07
  • java ThreadPoolExecutor 并發(fā)調(diào)用實(shí)例詳解

    java ThreadPoolExecutor 并發(fā)調(diào)用實(shí)例詳解

    這篇文章主要介紹了java ThreadPoolExecutor 并發(fā)調(diào)用實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評論