最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1、直接建一個工具類放入即可

/**
 * 發(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)文章

  • Spring?Boot?集成Elasticsearch模塊實現(xiàn)簡單查詢功能

    Spring?Boot?集成Elasticsearch模塊實現(xiàn)簡單查詢功能

    本文講解了Spring?Boot集成Elasticsearch采用的是ES模板的方式實現(xiàn)基礎(chǔ)查詢,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-06-06
  • 把spring boot項目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法)

    把spring boot項目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法)

    這篇文章主要介紹了把spring boot項目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法),然后在文章給大家提到了如何將Spring Boot項目打包部署到外部Tomcat,需要的朋友參考下吧
    2017-11-11
  • SpringBoot初始教程之Servlet、Filter、Listener配置詳解

    SpringBoot初始教程之Servlet、Filter、Listener配置詳解

    本篇文章主要介紹了SpringBoot初始教程之Servlet、Filter、Listener配置詳解,具有一定的參考價值,有興趣的可以了解一下
    2017-09-09
  • mybatis日志打印的兩款I(lǐng)DEA插件推薦

    mybatis日志打印的兩款I(lǐng)DEA插件推薦

    這篇文章主要給大家推薦介紹了關(guān)于mybatis日志打印的兩款I(lǐng)DEA插件,文中通過圖文以及實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-04-04
  • SpringMVC核心技術(shù)

    SpringMVC核心技術(shù)

    這篇文章主要介紹了SpringMVC入門實例,在springmvc入門教程里算是比較不錯的,結(jié)構(gòu)也比較完整,需要的朋友可以參考。希望可以幫助到你
    2021-07-07
  • Java核心庫實現(xiàn)AOP過程

    Java核心庫實現(xiàn)AOP過程

    給大家分享一下利用Java核心庫實現(xiàn)簡單的AOP的經(jīng)驗分享和教學(xué),需要的讀者們參考下吧。
    2017-12-12
  • Java的GUI編程之列表和組合框的設(shè)計使用

    Java的GUI編程之列表和組合框的設(shè)計使用

    這篇文章主要介紹了Java的GUI編程之列表和組合框的設(shè)計使用,是Java圖形界面編程中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • js實現(xiàn)拖拽拼圖游戲

    js實現(xiàn)拖拽拼圖游戲

    這篇文章主要為大家詳細介紹了js實現(xiàn)拖拽拼圖游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring Junit單元測試加載配置文件失敗問題

    Spring Junit單元測試加載配置文件失敗問題

    這篇文章主要介紹了Spring Junit加載配置文件失敗問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • 基于java編寫局域網(wǎng)多人聊天室

    基于java編寫局域網(wǎng)多人聊天室

    這篇文章主要為大家詳細介紹了基于java編寫局域網(wǎng)多人聊天室的相關(guān)資料,使用socket基于java編寫一個局域網(wǎng)聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論

仁寿县| 天台县| 鄂伦春自治旗| 沅陵县| 沭阳县| 丽江市| 乾安县| 谷城县| 隆昌县| 和田市| 遵化市| 布尔津县| 乐清市| 夹江县| 昭觉县| 云南省| 金山区| 文安县| 逊克县| 晋州市| 荔波县| 无棣县| 绩溪县| 常熟市| 深水埗区| 微博| 吉木乃县| 湘潭市| 原平市| 河北区| 新宁县| 蚌埠市| 正镶白旗| 清徐县| 淮滨县| 专栏| 黑水县| 商水县| 乌恰县| 宝应县| 大渡口区|