HttpClient和HttpUrlConnection的请求网路
2016-06-14 10:05
477 查看
请求网络
public class NetWorkUtils { public static String getString(String path){ HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(path); try { HttpResponse response = client.execute(get); if(response.getStatusLine().getStatusCode()==200){ String string = EntityUtils.toString(response.getEntity(), "utf-8"); return string; } } catch (Exception e) { e.printStackTrace(); } return null; }
public class NetWorkUtils { public static String getString(String path){ try { URL url =new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5*1000); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if(code==200){ InputStream inStream = conn.getInputStream(); int len=-1; byte[] arr = new byte[1024]; StringBuilder builder = new StringBuilder(); while((len=inStream.read(arr))!=-1){ builder.append(new String(arr,0,len)); } return builder.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; }
相关文章推荐
- Android官方开发文档Training系列课程中文版:网络操作之XML解析
- tcpdump抓包分析TCP三次握手过程
- Struts1.3 HTTP Status 500 No action config found for the specified url
- Android网络框架Volley
- 浏览器被定向至http://hao.169x.cn/?v=108
- Linux TCP队列相关参数的总结
- 【Linux C/C++】 第04讲 多进程TCP传输文件
- 在C#中带连接超时功能的TcpClient类
- HTTP协议详解(转载)
- 初识HTTP 1.1与HTTP 1.0
- OSI参考模型及TCP/IP参考模型
- Anroid搭建一个局域网Http服务器
- jiaocheng https://github.com/CarpenterLee/JCFInternals
- Android学习-使用Async-Http实现图片压缩并上传功能
- Startssl 现在就启用 HTTPS,免费的!
- 网络人远程监控软件(Netman旗舰版) v2.311官方版
- 香港服务器租用及香港服务器托管方法
- http://www.codeweblog.com/15个初学者必看的基础sql查询语句/
- Angular2 http服务
- Android HTTP请求用HttpUrlConnection与HttpClient比较