java使用post方式如何实现访问后台服务器

本篇文章给大家分享的是有关java使用post方式如何实现访问后台服务器,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,成都小程序开发,十载建站对成都汽车玻璃修复等多个行业,拥有丰富建站经验。

首先,上代码:

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);//转换成xml格式 
             
            String resultStr = Utils.httpPost(UrlString, payParamStr);//调用访问函数 
             
            //<span >resultStr 就是访问所得到的返回值 </span> 
          } 
          catch(Exception e) 
          { 
            e.printStackTrace(); 
          } 
        } 
      }; 
      // 必须异步调用 
      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方式如何实现访问后台服务器,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。

标题名称:java使用post方式如何实现访问后台服务器
标题URL:https://www.cdcxhl.com/article34/ghscpe.html

成都网站建设公司_创新互联,为您提供动态网站软件开发电子商务微信公众号品牌网站建设网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设