1、(完整版)JAVA利用HttpClient进行POST请求(HTTPS)JAVA利用HttpClient进行POST请求(HTTPS)分类:java(31)jsp(1)目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuerypost进行请求。但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的post请求是不允许跨域的。这时,就只能够用HttpClient包进行请求了,同时由于请求的URL是HTTPS的,为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程.1。写一个SSLClient类,继承至
2、HttpClientjavaview plaincopy1. importjava.security.cert.CertificateException;2. importjava.security。cert。X509Certificate;3. import。ssl。SSLContext;4. importjavax。net.ssl.TrustManager;5. import.ssl。X509TrustManager;6. importorg.apache。http.conn。ClientConnectionManager;7. importorg.apache.http。conn。sch
3、eme。Scheme;8. importorg。apache.http.conn.scheme。SchemeRegistry;9. importorg。apache。http。conn。ssl.SSLSocketFactory;10. importorg.apache.http。impl。client.DefaultHttpClient;11. /用于进行Https请求的HttpClient12. publicclassSSLClientextendsDefaultHttpClient13. publicSSLClient()throwsException14. super();15. SSL
4、Contextctx=SSLContext.getInstance(”TLS);16. X509TrustManagertm=newX509TrustManager()17. Override18. publicvoidcheckClientTrusted(X509Certificatechain,19. StringauthType)throwsCertificateException20. 21. Override22. publicvoidcheckServerTrusted(X509Certificatechain,23. StringauthType)throwsCertificat
5、eException24. 25. Override26. publicX509CertificategetAcceptedIssuers()27. returnnull;28. 29. ;30. ctx。init(null,newTrustManagertm,null);31. SSLSocketFactoryssf=newSSLSocketFactory(ctx,SSLSocketFactory。ALLOW_ALL_HOSTNAME_VERIFIER);32. ClientConnectionManagerccm=this.getConnectionManager();33. Scheme
6、Registrysr=ccm。getSchemeRegistry();34. sr。register(newScheme(”https,443,ssf));35. 36. javaview plaincopy1. importjava。security.cert.CertificateException;2. importjava。security。cert。X509Certificate;3. import。ssl。SSLContext;4. import。ssl。TrustManager;5. importjavax。net.ssl。X509TrustManager;6. importor
7、g.apache.http。conn.ClientConnectionManager;7. importorg.apache。http。conn.scheme.Scheme;8. importorg。apache.http.conn.scheme.SchemeRegistry;9. importorg。apache.http。conn。ssl.SSLSocketFactory;10. importorg.apache.http。impl。client。DefaultHttpClient;11. /用于进行Https请求的HttpClient12. publicclassSSLClientext
8、endsDefaultHttpClient13. publicSSLClient()throwsException14. super();15. SSLContextctx=SSLContext。getInstance(”TLS);16. X509TrustManagertm=newX509TrustManager()17. Override18. publicvoidcheckClientTrusted(X509Certificatechain,19. StringauthType)throwsCertificateException20. 21. Override22. publicvoi
9、dcheckServerTrusted(X509Certificatechain,23. StringauthType)throwsCertificateException24. 25. Override26. publicX509CertificategetAcceptedIssuers()27. returnnull;28. 29. ;30. ctx。init(null,newTrustManagertm,null);31. SSLSocketFactoryssf=newSSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VER
10、IFIER);32. ClientConnectionManagerccm=this。getConnectionManager();33. SchemeRegistrysr=ccm。getSchemeRegistry();34. sr。register(newScheme(”https,443,ssf));35. 36. 2。写一个利用HttpClient发送post请求的类javaview plaincopy1. importjava.util。ArrayList;2. importjava.util。Iterator;3. importjava。util。List;4. importjav
11、a。util。Map;5. importjava.util。Map。Entry;6. importorg.apache.http.HttpEntity;7. importorg。apache。http.HttpResponse;8. importorg。apache.http.NameValuePair;9. importorg.apache.http.client.HttpClient;10. importorg。apache。http。client.entity.UrlEncodedFormEntity;11. importorg.apache.http。client.methods。Ht
12、tpPost;12. importorg。apache.http.message.BasicNameValuePair;13. importorg.apache.http。util.EntityUtils;14. /15. *利用HttpClient进行post请求的工具类16. /17. publicclassHttpClientUtil18. publicStringdoPost(Stringurl,MapString,Stringmap,Stringcharset)19. HttpClienthttpClient=null;20. HttpPosthttpPost=null;21. St
13、ringresult=null;22. try23. httpClient=newSSLClient();24. httpPost=newHttpPost(url);25. /设置参数26. ListNameValuePairlist=newArrayListNameValuePair();27. Iteratoriterator=map.entrySet()。iterator();28. while(iterator.hasNext())29. Entryelem=(EntryString,String)iterator。next();30. list.add(newBasicNameVal
14、uePair(elem。getKey(),elem。getValue());31. 32. if(list.size()0)33. UrlEncodedFormEntityentity=newUrlEncodedFormEntity(list,charset);34. httpPost。setEntity(entity);35. 36. HttpResponseresponse=httpClient。execute(httpPost);37. if(response!=null)38. HttpEntityresEntity=response。getEntity();39. if(resEnt
15、ity!=null)40. result=EntityUtils.toString(resEntity,charset);41. 42. 43. catch(Exceptionex)44. ex。printStackTrace();45. 46. returnresult;47. 48. javaview plaincopy1. importjava。util.ArrayList;2. importjava。util。Iterator;3. importjava.util。List;4. importjava.util.Map;5. importjava。util.Map.Entry;6. i
16、mportorg。apache。http。HttpEntity;7. importorg.apache。http.HttpResponse;8. importorg.apache。http。NameValuePair;9. importorg。apache.http.client.HttpClient;10. importorg。apache.http。client.entity.UrlEncodedFormEntity;11. importorg.apache.http。client。methods.HttpPost;12. importorg.apache.http。message。Bas
17、icNameValuePair;13. importorg.apache.http。util.EntityUtils;14. /15. 利用HttpClient进行post请求的工具类16. /17. publicclassHttpClientUtil18. publicStringdoPost(Stringurl,MapString,Stringmap,Stringcharset)19. HttpClienthttpClient=null;20. HttpPosthttpPost=null;21. Stringresult=null;22. try23. httpClient=newSSLC
18、lient();24. httpPost=newHttpPost(url);25. /设置参数26. Listlist=newArrayListNameValuePair();27. Iteratoriterator=map。entrySet()。iterator();28. while(iterator.hasNext())29. Entryelem=(EntryString,String)iterator.next();30. list。add(newBasicNameValuePair(elem。getKey(),elem.getValue()));31. 32. if(list.siz
19、e()0)33. UrlEncodedFormEntityentity=newUrlEncodedFormEntity(list,charset);34. httpPost.setEntity(entity);35. 36. HttpResponseresponse=httpClient.execute(httpPost);37. if(response!=null)38. HttpEntityresEntity=response。getEntity();39. if(resEntity!=null)40. result=EntityUtils.toString(resEntity,chars
20、et);41. 42. 43. catch(Exceptionex)44. ex。printStackTrace();45. 46. returnresult;47. 48. 3.调用post请求的测试代码javaview plaincopy1. importjava.util.HashMap;2. importjava。util。Map;3. /对接口进行测试4. publicclassTestMain5. privateStringurl=https:/192.168。1。101/;6. privateStringcharset=utf-8;7. privateHttpClientUtil
21、httpClientUtil=null;8. 9. publicTestMain()10. httpClientUtil=newHttpClientUtil();11. 12. 13. publicvoidtest()14. StringhttpOrgCreateTest=url+”httpOrg/create”;15. MapcreateMap=newHashMapString,String();16. createMap。put(authuser”,”*”);17. createMap.put(”authpass,*);18. createMap.put(orgkey,”*”);19. c
22、reateMap。put(orgname”,*”);20. StringhttpOrgCreateTestRtn=httpClientUtil。doPost(httpOrgCreateTest,createMap,charset);21. System.out.println(result:”+httpOrgCreateTestRtn);22. 23. 24. publicstaticvoidmain(Stringargs)25. TestMainmain=newTestMain();26. main.test();27. 28. javaview plaincopy1. importjava
23、。util。HashMap;2. importjava.util.Map;3. /对接口进行测试4. publicclassTestMain5. privateStringurl=https:/192。168。1.101/”;6. privateStringcharset=”utf8”;7. privateHttpClientUtilhttpClientUtil=null;8. 9. publicTestMain()10. httpClientUtil=newHttpClientUtil();11. 12. 13. publicvoidtest()14. StringhttpOrgCreate
24、Test=url+httpOrg/create”;15. MapString,StringcreateMap=newHashMapString,String();16. createMap。put(authuser,”*);17. createMap.put(authpass”,”*”);18. createMap.put(orgkey,”*);19. createMap。put(”orgname”,”*);20. StringhttpOrgCreateTestRtn=httpClientUtil。doPost(httpOrgCreateTest,createMap,charset);21. System。out.println(result:+httpOrgCreateTestRtn);22. 23. 24. publicstaticvoidmain(Stringargs)25. TestMainmain=newTestMain();26. main.test();27. 28. httpClient4.2的jar包下载路径:http:/
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100