簡單粗暴套娃模式組json發送https請求


  各位童鞋大家好,向來簡單粗暴的鐵柱兄給大家來玩一手套娃模式來組Json數據,不說別的,無腦套。

  當然,這一手比較適合臨場用一下,若長期用的話建議搞一套適用的框架,只管set就好了。話不多說開始上課。

  套娃模式這個顧名思義啊,就是一層一層的往里面套就好了,特舒服。先上一手代碼:

發送https請求的代碼我是隨便搜的,這一套如果對你適用的話就直接復制過去了,套娃這套代碼對發什么請求都無所謂,只要對方要求的是json格式的。

package test;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
 
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;


public class SSLClient   extends DefaultHttpClient {

	 public SSLClient() throws Exception {//網上搜的發https的方法
	        super();
	        //傳輸協議需要根據自己的判斷
	        SSLContext ctx = SSLContext.getInstance("TLS");
	        X509TrustManager tm = new X509TrustManager() {
	            @Override
	            public void checkClientTrusted(X509Certificate[] chain,
	                                           String authType) throws CertificateException {
	            }
	 
	            @Override
	            public void checkServerTrusted(X509Certificate[] chain,
	                                           String authType) throws CertificateException {
	            }
	 
	            @Override
	            public X509Certificate[] getAcceptedIssuers() {
	                return null;
	            }
	        };
	        ctx.init(null, new TrustManager[]{tm}, null);
	        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
	        ClientConnectionManager ccm = this.getConnectionManager();
	        SchemeRegistry sr = ccm.getSchemeRegistry();
	        sr.register(new Scheme("https", 443, ssf));
	    }
	 public static String doPost(String url, String map, String charset) {
	        org.apache.http.client.HttpClient httpClient = null;
	        HttpPost httpPost = null;
	        String result = null;
	        try {
	            httpClient = new SSLClient();
	            httpPost = new HttpPost(url);
	            //設置參數
	            httpPost.addHeader("Accept", "application/json");
	            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
	            StringEntity stringEntity = new StringEntity(map);
	            stringEntity.setContentEncoding("UTF-8");
	            stringEntity.setContentType("application/json");
	            httpPost.setEntity(stringEntity);
	            HttpResponse response = httpClient.execute(httpPost);
	            if (response != null) {
	                HttpEntity resEntity = response.getEntity();
	                if (resEntity != null) {
	                    result = EntityUtils.toString(resEntity, charset);
	                }
	            }
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
	        return result;
	 }
	 private static String url = "https://xx.xx.xx.xxx:xxxx/xxxx/xxxx";//填寫需要發送請求的地址 https后面跟ip跟端口跟地址
	 private static String charset = "utf-8";

	 public static void main(String[] args) {
		   
	    /**
	     * new幾個JSONObject出來作為套筒
	     * 需要幾層就new幾個
	     */
	        JSONObject json=new  JSONObject();//最大的套筒
	    	JSONObject json1=new JSONObject();
	    	JSONObject json2=new JSONObject();
	    	//json1、json2作為第二層套筒 這些數據放自己需要的即可
	    	json1.put("txnCode", "GWS00004");
	    	json1.put("reqDate", "20201102");
	    	json1.put("reqTime", "101532");
	    	json1.put("channelId", "  stockapp");
	    	json1.put("traceNo", "9e4124f5b1c145c18b698fb7d5628002");
	    	json.put("header", json1);
	    	json2.put("ciNo", "8000001397");
	    	json.put("body",json2 );
	        String encryptStr = json.toString();
	        System.out.println("encryptStr:" + encryptStr);
	        String httpOrgCreateTestRtn = doPost(url, encryptStr, charset);//丟去發送剛組的數據
	        System.out.println("result:" + httpOrgCreateTestRtn);//返回數據
	}
}

  組起來其實是很容易的,有了思路隨便套,隨便多少層,隨意套。

 

 

 

encryptStr:{"header":{"txnCode":"GWS00004","reqDate":"20201102","traceNo":"9e4124f5b1c145c18b698fb7d5628002","reqTime":"101532","channelId":"  stockapp"},"body":{"ciNo":"8000001397"}}
result:{"header":{"txnCode":"GWS00004","resDate":"20201102","resTime":"051127","retCode":"DD6010","errMsg":"DD6010","traceNo":""}}

 

  

 

 

   這套方法也適用Map,解釋啥的我一如既往的丟注釋里了,有不明白的地方歡迎提問。謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM