詳解java中通過post方式訪問后臺服務(wù)器
更新時間:2017年03月24日 09:56:34 作者:少壯不努力-老大敲代碼
本篇文章主要介紹了詳解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)文章
java自定義任務(wù)類定時執(zhí)行任務(wù)示例 callable和future接口使用方法
Callable是類似于Runnable的接口,實現(xiàn)Callable接口的類和實現(xiàn)Runnable的類都是可被其它線程執(zhí)行的任務(wù)2014-01-01
nacos服務(wù)注冊服務(wù)發(fā)現(xiàn)依賴配置詳解
這篇文章主要為大家介紹了nacos服務(wù)注冊服務(wù)發(fā)現(xiàn)依賴配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
使用Idea maven創(chuàng)建Spring項目過程圖解
這篇文章主要介紹了使用Idea maven創(chuàng)建Spring項目過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程
這篇文章主要介紹了SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
springboot+hutool批量生成二維碼壓縮導(dǎo)出功能
這篇文章主要介紹了springboot+hutool批量生成二維碼壓縮導(dǎo)出功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10

