최근에 고객의 인터페이스에 래퍼를 만들고 회사의 다른 시스템 호출을 사용해야합니다. 클라이언트 인터페이스는 HTTP URL을 사용하여 구현됩니다. httpclient 패키지를 사용하여 요청을하고 싶습니다. 동시에, 요청 된 URL은 HTTPS이므로 인증서가 필요하지 않기 위해 클래스를 사용하여 defaulthttpclient 클래스를 상속하고 확인 프로세스를 무시합니다.
1. sslclient 클래스를 작성하고 httpclient에 상속받습니다.
패키지 com.pcmall.service.sale.miaomore.impl; java.security.cert.certificateException 가져 오기; Java.security.cert.x509Certificate 가져 오기; import javax.net.ssl.sslcontext; import javax.net.ssl.trustmanager; import javax.net.ssl.x509trustmanager; 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; // httpscclient https 요청을 요청하는 데 사용되는 httpclient public class sslclient extends defaulthtttpclient {public sslclient () throws exception {super (); sslcontext ctx = sslcontext.getinstance ( "tls"); x509trustmanager tm = new x509trustmanager () {@override public void checkclientTrusted (x509certificate [] 체인, String authtype) 추력 증명서 exception {} @override public void checkserverTrusted (x509certificate, string upplial) x509certificate [] getAcceptedIssuers () {return null; }}; ctx.init (null, new trustmanager [] {tm}, null); sslsocketfactory ssf = new sslsocketfactory (ctx, sslsocketfactory.alow_all_hostname_verifier); ClientConnectionManager CCM = this.GetConnectionManager (); schemeregistry sr = ccm.getSchemeregistry (); sr.register (새로운 체계 ( "https", 443, ssf)); }}2. httpclient를 사용하여 게시물 요청을 보내는 클래스를 작성하십시오.
패키지 com.pcmall.service.sale.miaomore.impl; java.util.arraylist 가져 오기; import java.util.iterator; Java.util.list 가져 오기; java.util.map import; Java.util.map.entry 가져 오기; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.message.basicnamevaluepair; import org.apache.http.util.entityutils; /** httpclient*/ public class httpclientUtil {public string dopost (문자열 URL, MAP <String, String, Map, String Charset) {httpclient httpclient = null; httppost httppost = null; 문자열 결과 = null; try {httpclient = new sslclient (); httppost = 새로운 httppost (URL); // 매개 변수 목록을 설정 <NamesValuePair> list = new ArrayList <NamesValuePair> (); iterator iterator = map.entryset (). iterator (); while (iterator.hasnext ()) {entry <string, string> elem = (Entry <String, String>) iterator.next (); list.add (new BasicNameValuePair (elem.getKey (), elem.getValue ())); } if (list.size ()> 0) {urlencodedformentity entity = 새로운 urlencodedformentity (list, charset); httppost.setentity (엔티티); } httpresponse 응답 = httpclient.execute (httppost); if (inentity! = null) {httpentity inentity = response.getentity (); if (inentity! = null) {result = entityutils.toString (분노, 숯); }}}} catch (Exception Ex) {ex.printstacktrace (); } 반환 결과; }}3. 사후 요청은 테스트 코드에 전화하십시오
패키지 com.pcmall.service.sale.miaomore.impl; java.util.hashmap import; java.util.map import; // 인터페이스 공용 클래스 테스트 테스트 테스트 {private String url = "https://xxx.xxx.xxx/"; 개인 문자열 charset = "UTF-8"; 개인 httpclientutil httpclientutil = null; public testmain () {httpclientUtil = new httpclientUtil (); } public void test () {문자열 httporgcreateTest = url + "xxx/xxx/delivery"; map <string, string> createmap = new Hashmap <String, String> (); createmap.put ( "Delivery_Code", "1D1QZ22Z22SM21A"); createmap.put ( "타임 스탬프", "1479198840000"); createmap.put ( "부호", "f2109c333f3eade929f932e89703fa0f683d43eb"); 문자열 httporgcreatetestrtn = httpclientutil.dopost (httporgcreateTest, createmap, charset); System.out.println ( "결과 :"+httporgcreatetestrtn); } public static void main (String [] args) {testmain main = new testmain (); main.test (); }}처음에는 BasicNameValuePair의 사용법을 이해하지 못했습니다. 나중에, 나는 그것을 천천히 탐구하고 BasicNameValuePair가 키 값 쌍을 저장하는 클래스라는 것을 알았습니다. 새 키 및 값 값을 추가 할 때는 자동으로 HTTP 형식으로 교체합니다. https://xxx.xxx.xxx/xxx/xxxx/delivery?delivery_code=dqzzsm2axtamp=1479198840000&sign=f209c33feade99f93e8970fa0f68d3eb. 우리는 스스로와 일치 할 필요가 없습니다. 나는 개인적으로 사용하기가 매우 편리하고 정확하다고 생각합니다. 나는 그것이 모두를 도울 수 있기를 바랍니다!
위는 편집자가 가져온 Java Post Request (HTTPS) 인스턴스의 전체 내용입니다. 모두가 wulin.com을 지원하기를 바랍니다