真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java使用post方式如何實(shí)現(xiàn)訪問后臺(tái)服務(wù)器

本篇文章給大家分享的是有關(guān)java使用post方式如何實(shí)現(xiàn)訪問后臺(tái)服務(wù)器,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

成都創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開發(fā),軟件開發(fā),成都小程序開發(fā),十載建站對(duì)成都汽車玻璃修復(fù)等多個(gè)行業(yè),擁有豐富建站經(jīng)驗(yàn)。

首先,上代碼:

Runnable payRunnable = new Runnable()  
      { 
        @Override 
        public void run()  
        { 
          try 
          {   
            HashMap payParamMap = new HashMap(); 
            payParamMap.put("canshu" ,"woshicanshu" ); 
            String payParamStr = XmlUtils.toXml(payParamMap);//轉(zhuǎn)換成xml格式 
             
            String resultStr = Utils.httpPost(UrlString, payParamStr);//調(diào)用訪問函數(shù) 
             
            //resultStr 就是訪問所得到的返回值  
          } 
          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();  
      }  
    } 
}

以上就是java使用post方式如何實(shí)現(xiàn)訪問后臺(tái)服務(wù)器,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


網(wǎng)頁題目:java使用post方式如何實(shí)現(xiàn)訪問后臺(tái)服務(wù)器
網(wǎng)站網(wǎng)址:http://weahome.cn/article/ghscpe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部