生活随笔
收集整理的這篇文章主要介紹了
发送http和https请求工具类 Json封装数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在一些業(yè)務(wù)中我們可要調(diào)其他的接口(第三方的接口) 這樣就用到我接下來用到的工具類。 用這個(gè)類需要引一下jar包的坐標(biāo)
< dependency> < groupId> org
. jsoup
< / groupId
> < artifactId> jsoup
< / artifactId
> < version> 1.11 .3 < / version
> < / dependency
>
引好坐標(biāo)后就可以使用這個(gè)工具類了;
Connection
. Response post
= HttpUtils
. post ( "http://localhost:10090/Controller/httpToJson" , str
) ;
String body
= post
. body ( ) ;
System
. out
. println ( "響應(yīng)報(bào)文:" + body
) ;
str代表你需要傳的參數(shù) 一般是json類型的數(shù)據(jù)
訪問成功后一般會(huì)返回你一個(gè)json的數(shù)據(jù),但是這個(gè)數(shù)據(jù)有一些問題,它會(huì)進(jìn)行轉(zhuǎn)譯。我把這個(gè)字符串進(jìn)行了一些處理。
{ "result" : "{\"FoodID\":\"A66J54DW9IKJ75U3\",\"FoodName\":\"桃子\",\"FoodBrand\":\"龍冠\",\"FoodPlace\":\"吉林\",\"FoodText\":\"四打擊我家的娃哦哦囧\",\"FoodImg\":\"sdasdasdasd\",\"FoodProInfo\":[{\"ProjectName\":\"澆水\",\"UserName\":\"周銳\",\"OperationDescribe\":\"給桃子樹澆水茁壯成長\",\"ImgUrl\":\"asdlpalplpd23\",\"OperationTm\":\"20200616103856\"}],\"FoodPackInfo\":null,\"FoodDetectionInfo\":null,\"FoodLogInfo\":null}" , "code" : 0 , "message" : "查詢智能合約成功" }
對這個(gè)串進(jìn)行替換等一些操作,然后把它轉(zhuǎn)成json對象。通過json工具類把它自動(dòng)封裝到實(shí)體類型中, (在進(jìn)行封裝實(shí)體的時(shí)候一定要注意,json串的屬性和實(shí)體的屬性要完全相同)否則會(huì)報(bào)錯(cuò)的(避免入坑)
Connection
. Response post
= HttpUtils
. post ( "http://localhost:10090/fabricController/queryChaincode" , str
) ; String body
= post
. body ( ) ; String replace
= body
. replace ( "\\\"" , "\"" ) ; String replace1
= replace
. replace ( ":\"{" , ":{" ) ; String replace2
= replace1
. replace ( "}\"," , "}," ) ; String replace3
= replace2
. replace ( "{\"result\":" , "" ) ; String replace4
= replace3
. replace ( ",\"code\":0,\"message\":\"查詢智能合約成功\"}" , "" ) ; com
. alibaba
. fastjson
. JSONObject jsonObject
= com
. alibaba
. fastjson
. JSONObject
. parseObject ( replace4
) ; FoodAllInfo foodAllInfo
= JSON
. parseObject ( replace4
, FoodAllInfo
. class ) ;
package com
. gblfy
. order
. utils
; import org
. jsoup
. Connection
;
import org
. jsoup
. Connection
. Response
;
import org
. jsoup
. Jsoup
; import javax
. net
. ssl
. *
;
import java
. io
. *
;
import java
. security
. SecureRandom
;
import java
. security
. cert
. CertificateException
;
import java
. security
. cert
. X509Certificate
;
import java
. util
. ArrayList
;
import java
. util
. List
;
import java
. util
. Map
;
import java
. util
. Map
. Entry
;
import java
. util
. Set
; public class HttpUtils { private static final int TIME_OUT
= 120000 ; private static final String HTTPS
= "https" ; public static Response
get ( String url
) throws IOException
{ return get ( url
, null
) ; } public static Response
get ( String url
, Map
< String, String> headers
) throws IOException
{ if ( null
== url
|| url
. isEmpty ( ) ) { throw new RuntimeException ( "The request URL is blank." ) ; } if ( url
. startsWith ( HTTPS
) ) { getTrust ( ) ; } Connection connection
= Jsoup
. connect ( url
) ; connection
. method ( Connection
. Method
. GET
) ; connection
. timeout ( TIME_OUT
) ; connection
. ignoreHttpErrors ( true ) ; connection
. ignoreContentType ( true ) ; if ( null
!= headers
) { connection
. headers ( headers
) ; } Response response
= connection
. execute ( ) ; return response
; } public static Response
post ( String url
, String params
) throws IOException
{ return doPostRequest ( url
, null
, null
, null
, params
) ; } public static Response
post ( String url
, Map
< String, String> headers
, Map
< String, String> params
, String flag
) throws IOException
{ return doPostRequest ( url
, headers
, params
, null
, null
) ; } public static Response
post ( String url
, Map
< String, String> headers
) throws IOException
{ return doPostRequest ( url
, headers
, null
, null
, null
) ; } public static Response
post ( String url
, Map
< String, String> paramMap
, Map
< String, File> fileMap
) throws IOException
{ return doPostRequest ( url
, null
, paramMap
, fileMap
, null
) ; } private static Response
doPostRequest ( String url
, Map
< String, String> headers
, Map
< String, String> paramMap
, Map
< String, File> fileMap
, String jsonParams
) throws IOException
{ if ( null
== url
|| url
. isEmpty ( ) ) { throw new RuntimeException ( "The request URL is blank." ) ; } if ( url
. startsWith ( HTTPS
) ) { getTrust ( ) ; } Connection connection
= Jsoup
. connect ( url
) ; connection
. method ( Connection
. Method
. POST
) ; connection
. timeout ( TIME_OUT
) ; connection
. ignoreHttpErrors ( true ) ; connection
. ignoreContentType ( true ) ; if ( null
!= headers
) { connection
. headers ( headers
) ; } List
< InputStream> inputStreamList
= null
; try { if ( null
!= fileMap
&& ! fileMap
. isEmpty ( ) ) { inputStreamList
= new ArrayList < InputStream> ( ) ; InputStream in
= null
; File file
= null
; Set
< Entry
< String, File> > set
= fileMap
. entrySet ( ) ; for ( Entry
< String, File> e
: set
) { file
= e
. getValue ( ) ; in
= new FileInputStream ( file
) ; inputStreamList
. add ( in
) ; connection
. data ( e
. getKey ( ) , file
. getName ( ) , in
) ; } } else if ( null
!= jsonParams
&& ! jsonParams
. isEmpty ( ) ) { connection
. header ( "Content-Type" , "application/json;charset=UTF-8" ) ; connection
. requestBody ( jsonParams
) ; } else { connection
. header ( "Content-Type" , "application/x-www-form-urlencoded" ) ; } if ( null
!= paramMap
&& ! paramMap
. isEmpty ( ) ) { connection
. data ( paramMap
) ; } Response response
= connection
. execute ( ) ; return response
; } catch ( FileNotFoundException e
) { throw e
; } catch ( IOException e
) { throw e
; } finally { if ( null
!= inputStreamList
) { for ( InputStream in
: inputStreamList
) { try { in
. close ( ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } } } } } private static void getTrust ( ) { try { HttpsURLConnection
. setDefaultHostnameVerifier ( new HostnameVerifier ( ) { @Override public boolean verify ( String hostname
, SSLSession session
) { return true ; } } ) ; SSLContext context
= SSLContext
. getInstance ( "TLS" ) ; context
. init ( null
, new X509TrustManager [ ] { 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 new X509Certificate [ 0 ] ; } } } , new SecureRandom ( ) ) ; HttpsURLConnection
. setDefaultSSLSocketFactory ( context
. getSocketFactory ( ) ) ; } catch ( Exception e
) { e
. printStackTrace ( ) ; } }
}
總結(jié)
以上是生活随笔 為你收集整理的发送http和https请求工具类 Json封装数据 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。