詳解java中通過post方式訪問后臺服務(wù)器
最近在學(xué)習(xí)java中通過post方式訪問后臺服務(wù)器,那么今天也算個學(xué)習(xí)筆記吧!
首先,上代碼:
Runnable payRunnable = new Runnable() { @Override public void run() { try { HashMap<String, String> payParamMap = new HashMap<String, String>(); payParamMap.put("canshu" ,"woshicanshu" ); String payParamStr = XmlUtils.toXml(payParamMap);//轉(zhuǎn)換成xml格式 String resultStr = Utils.httpPost(UrlString, payParamStr);//調(diào)用訪問函數(shù) //<span style="font-family: Arial, Helvetica, sans-serif;">resultStr 就是訪問所得到的返回值 </span> } catch(Exception e) { e.printStackTrace(); } } }; // 必須異步調(diào)用 Thread payThread = new Thread(payRunnable); payThread.start();
其次,上代碼:
public class Utils { private static final String TAG = "woshiTag"; public static String httpPost(String url, String entity) { if (url == null || url.length() == 0) { Log.e(TAG, "httpPost, url is null"); return null; } HttpClient httpClient = getNewHttpClient(); HttpPost httpPost = new HttpPost(url); try { httpPost.setEntity(new StringEntity(entity, HTTP.UTF_8)); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpResponse resp = httpClient.execute(httpPost); if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { Log.e(TAG, "httpGet fail, status code = " + resp.getStatusLine().getStatusCode()); return null; } return new String(EntityUtils.toByteArray(resp.getEntity())); } catch (Exception e) { Log.e(TAG, "httpPost exception, e = " + e.getMessage()); e.printStackTrace(); return null; } } private static HttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring事務(wù)@Transactional失效原因及解決辦法小結(jié)
今天就跟大家聊聊有關(guān)spring中@Transactional失效原因及解決辦法小結(jié),主要從三個方面考慮,具有一定的參考價值,感興趣的可以了解一下2023-08-08java Swing JFrame框架類中setDefaultCloseOperation的參數(shù)含義與用法示例
這篇文章主要介紹了java Swing JFrame框架類中setDefaultCloseOperation的參數(shù)含義與用法,結(jié)合實例形式分析了Swing組件的JFrame框架類中setDefaultCloseOperation方法的簡單使用技巧,需要的朋友可以參考下2017-11-11SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因
spring-boot-maven-plugin是spring boot提供的maven打包插件,本文主要介紹了SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因,具有一定的參考價值,感興趣的可以了解一下2024-03-03SpringBoot集成Jpa對數(shù)據(jù)進(jìn)行排序、分頁、條件查詢和過濾操作
這篇文章主要介紹了SpringBoot集成Jpa對數(shù)據(jù)進(jìn)行排序、分頁、條件查詢和過濾操作,主要使用Jpa連接數(shù)據(jù)庫對數(shù)據(jù)進(jìn)行排序、分頁、條件查詢和過濾操作,需要的朋友可以參考下2023-05-05springboot中EasyPoi實現(xiàn)自動新增序號的方法
本文主要介紹了EasyPoi實現(xiàn)自動新增序號,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09java環(huán)境變量path和classpath的配置
這篇文章主要為大家詳細(xì)介紹了java系統(tǒng)環(huán)境變量path和classpath的配置過程,感興趣的小伙伴們可以參考一下2016-07-07