淘吧仔,玩美情人,将丈夫埋自家地板下
最近在学习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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论