http请求封装
来源:互联网 发布:快手淘宝优惠卷骗局 编辑:程序博客网 时间:2024/05/29 05:14
import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;public class HttpUtil {public static final String HOST="http://192.168.0.115/";//通过url获得HttpGet对象public static HttpGet getHttpGet(String url){//实例化HttpGetHttpGet request = new HttpGet(url);return request;}//通过URL获得HttpPost对象public static HttpPost getHttpPost(String url){//实例化HttpPost HttpPost request = new HttpPost(url); return request;}//通过HttpGet获得HttpResponse对象public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException,IOException{ //实例化HttpResponseHttpResponse response = new DefaultHttpClient().execute(request);return response;}//通过HttpPost获得HttpResponse对象public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException,IOException{//实例化HttpResponseHttpResponse response = new DefaultHttpClient().execute(request);return response;}//通过url发送post请求,返回请求结果public static String queryStringForPost(String url){//获得HttpPost实例HttpPost request = HttpUtil.getHttpPost(url);String result = null;try{//获得HttpResponse实例HttpResponse response = HttpUtil.getHttpResponse(request); //判断是否请求成功//if(response.getStatusLine().getStatusCode() ==200){//获得返回结果result = EntityUtils.toString(response.getEntity());return result;//}}catch(ClientProtocolException e){e.printStackTrace(); result = "网络异常!"; return result;}catch(IOException e){e.printStackTrace();result = "网络异常!";return result;} }//通过url发送get请求,返回请求结果public static String queryStringForGet(String url){//获得HttpGet实例HttpGet request = HttpUtil.getHttpGet(HOST+url);String result = null;try{//获得HttpResponse实例HttpResponse response = HttpUtil.getHttpResponse(request);//判断是否请求成功//if(response.getStatusLine().getStatusCode()==200){//获得返回结果result = EntityUtils.toString(response.getEntity());//result = EntityUtils.toString(response.getEntity(),"UTF-8");return result;//}}catch(ClientProtocolException e){e.printStackTrace();result = "网络异常!";return result;}catch(IOException e){e.printStackTrace();result = "网络异常!";return result;}//return null;}//通过HttpPost发送Post请求,返回请求结果public static String queryStringForPost(HttpPost request){String result = null;try{//获得HttpResponse实例HttpResponse response = HttpUtil.getHttpResponse(request);//判断是否请求成功//if(response.getStatusLine().getStatusCode()==200){//获得请求结果result = EntityUtils.toString(response.getEntity());return result;//}}catch(ClientProtocolException e){e.printStackTrace();result = "网络异常!";return result;}catch(IOException e){e.printStackTrace();result = "网络异常!";return result;}//return null;}//通过HttpGet发送Get请求,返回请求结果public static String queryStringForGet(HttpGet request){String result = null;try{ //获得HttpResponse实例HttpResponse response = HttpUtil.getHttpResponse(request);//判断是否请求成功//if(response.getStatusLine().getStatusCode()==200){//获得请求结果result = EntityUtils.toString(response.getEntity());return result;//}}catch(ClientProtocolException e){e.printStackTrace();result = "网络异常!";return result;}catch(IOException e){e.printStackTrace();result = "网络异常!";return result;}//return null;}public static InputStream GetInputStreamFromURL(String urlstr){ HttpURLConnection connection; URL url; InputStream stream=null; try{ url=new URL(urlstr); connection =(HttpURLConnection)url.openConnection(); connection.connect(); stream = connection.getInputStream(); }catch(MalformedURLException e){ e.printStackTrace(); }catch(IOException e1){ e1.printStackTrace(); } return stream; }}
0 0
- Android封装Http请求
- Block封装http请求
- http请求封装
- 简单封装 HTTP 请求
- go http 请求封装
- http请求方法封装
- http封装ajax请求
- Java HTTP请求类封装
- Android Http网络请求封装
- Android中封装Http请求
- xUtils封装http请求操作
- ajax+promise 封装http请求
- android Http异步请求封装
- 封装ng2的Http请求
- 封装js发送http请求
- Http封装请求工具类
- angular中的http请求封装
- 【微信小程序】封装http请求
- 【索引】Codeforces Round #278
- 偶遇Unslider
- Linux-->问题解决-->/dev/mapper/VolGroup-lv_root:
- 探讨—设备商应当如何使用开源代码
- 浅析busybox查找命令和调用相应命令函数的实现流程框架
- http请求封装
- 线段树
- hdu 5167
- 翻译经典之《Cisco Lan Switching》第六章 理解生成树(一): 本章内容
- 《人人都是产品经理》读后感二
- Android WebView加载服务端页面AJAX认证失败问题
- Apache2.4+ PHP5.6 搭建PHP开发环境笔记
- Android之Handler更新UI总结
- 工厂设计模式