您好,欢迎访问一九零五行业门户网

java操作new HttpPost(URL)和 new URL(shixun_ip) 获取流

httpclient:
private static httpclient httpclient; static { if (httpclient == null) { httpparams params = new basichttpparams(); // 设置一些基本参数 httpprotocolparams.setversion(params, httpversion.http_1_1); // 超时设置 /* 从连接池中取连接的超时时间 */ connmanagerparams.settimeout(params, 1000); /* 连接超时 */ httpconnectionparams.setconnectiontimeout(params, 2000); /* 请求超时 */ httpconnectionparams.setsotimeout(params, 4000); schemeregistry schreg = new schemeregistry(); schreg.register(new scheme("http", plainsocketfactory .getsocketfactory(), 80)); schreg.register(new scheme("https", sslsocketfactory .getsocketfactory(), 443)); // 使用线程安全的连接管理来创建httpclient clientconnectionmanager conmgr = new threadsafeclientconnmanager( params, schreg); httpclient = new defaulthttpclient(conmgr, params); } }@suppresswarnings("unchecked") private <t> t getjsonobjectfromurl(string urlstr, list<namevaluepair> params, class<t> classname) throws exception{ t object = null; inputstream in = null; try { httppost httppost = new httppost(urlstr); if (params != null) { // list<namevaluepair> nvps = new arraylist<namevaluepair>(); // for (entry<string, string> entry : params.entryset()) { // nvps.add(new basicnamevaluepair(entry.getkey(), entry // .getvalue())); // } httppost.setentity(new urlencodedformentity(params, http.utf_8)); } httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); if(entity!=null){ in= entity.getcontent(); //之前没使用这个造成了大量异常抛出 } /*string s = entityutils.tostring(in, "utf-8"); if (entity != null) { entityutils.consume(entity); }*/ string s = inputstream2string(in,"utf-8"); jsonresult jsonresult = (jsonresult)jsonobject.parseobject(s, jsonresult.class); if(jsonresult.getcode() == 200){ object obj = jsonresult.getdata(); if(obj != null){ if(classname.isinstance(obj)){ object = (t)(obj); } else if(obj instanceof jsonobject){ object= jsonobject.parseobject(obj.tostring(), classname); } } }else{ throw new exception(jsonresult.getmsg()); } if(in!=null){ in.close(); } } catch (exception e) { throw e; } return object; }
new url():
try{ <span style="white-space:pre"> </span>//省略 url shixun = new url(url); inputstream in = shixun.openstream(); long end = system.currenttimemillis(); if((end-start)<=5000){ //5s内不响应就跳过 string ret = cn.com.jsoft.jframe.utils.stringutils.tostring(in, "utf-8"); jsonobject obj = jsonobject.fromobject(ret); if(obj!=null&&obj.getjsonobject("info")!=null&&obj.containskey("info")==true){ jsonobject info = obj.getjsonobject("info"); jsonarray data = info.getjsonarray("data"); if(data!=null&&data.size()>0){ for(int i=0;i<data.size();i++){ jsonobject entry = data.getjsonobject(i); } } } in.close(); } }catch (exception e) { e.printstacktrace(); }
java将inputstream转为string
public static string inputstream2string(inputstream is, string charset) { bytearrayoutputstream baos = null; try { baos = new bytearrayoutputstream(); int i = -1; while ((i = is.read()) != -1) { baos.write(i); } return baos.tostring(charset); } catch (ioexception e) { e.printstacktrace(); logger.error("filewrutil.inputstream2string(inputstream is, string charset) occur error:" + e.getmessage()); } finally { if (null != baos) { try { baos.close(); } catch (ioexception e) { e.printstacktrace(); logger.error("filewrutil.inputstream2string(inputstream is, string charset) occur error:" + e.getmessage()); } baos = null; } } return null; }
以上就是java操作new httppost(url)和 new url(shixun_ip) 获取流的详细内容。
其它类似信息

推荐信息