欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解java中通过post方式访问后台服务器

程序员文章站 2024-03-03 14:52:52
最近在学习java中通过post方式访问后台服务器,那么今天也算个学习笔记吧! 首先,上代码: runnable payrunnable = new r...

最近在学习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 style="font-family: arial, helvetica, sans-serif;">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();  
      }  
    } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。