简单的POST请求和get请求
来源:互联网 发布:知乎的运营模式 编辑:程序博客网 时间:2024/05/29 10:54
package com.dp.util;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;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.message.BasicNameValuePair;import org.apache.http.params.CoreConnectionPNames;import org.apache.http.params.HttpConnectionParams;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import com.android.volley.DefaultRetryPolicy;import com.android.volley.Request.Method;import com.android.volley.Response.ErrorListener;import com.android.volley.Response.Listener;import com.android.volley.toolbox.StringRequest;public class HttpClientUtils { /** * 自己实现HTTP请求 HttpClient POST请求 */ public static String post(String url, Map<String, String> params) { try { List<NameValuePair> postParams = new ArrayList<NameValuePair>(); StringBuffer auth = new StringBuffer(); StringBuffer token = new StringBuffer(); for (Entry<String, String> entry : params.entrySet()) { if ("".equals(auth.toString())) { auth.append(entry.getKey()); } else { auth.append("," + entry.getKey()); } token.append(entry.getValue()); postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } // 创建POST请求 HttpPost request = new HttpPost(url); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams,HTTP.UTF_8); request.setEntity(entity); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000); HttpResponse response = client.execute(request); if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){ throw new RuntimeException("请求失败"); } HttpEntity resEntity = response.getEntity(); return (resEntity == null) ? null : EntityUtils.toString(resEntity, HTTP.UTF_8); } catch (Exception e) { return null; } } /** * 自己实现的Http请求 HttpClient get请求 */ public static String get(String url){ HttpGet httpGet = new HttpGet(url); //设置连接超时或响应超时 HttpConnectionParams.setConnectionTimeout(httpGet.getParams(), 15000); HttpConnectionParams.setSoTimeout(httpGet.getParams(), 15000); //创建一个网络访问处理对象 HttpClient httpClient = new DefaultHttpClient(); try{ HttpResponse response = httpClient.execute(httpGet); //发起GET请求 if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){ throw new RuntimeException("请求失败"); } HttpEntity resEntity = response.getEntity(); return (resEntity == null) ? null : EntityUtils.toString(resEntity, HTTP.UTF_8); }catch(Exception e){ return null; }finally{ //释放网络连接资源 httpClient.getConnectionManager().shutdown(); } }}
0 0
- 简单的POST请求和get请求
- okgo简单的GET请求 和POST请求
- Get请求和Post请求的区别
- POST请求和GET请求的区别:
- Get请求和Post请求的区别
- GET请求和POST请求的区别
- get请求和post请求的区别
- get 请求和post请求的区别
- get请求和post请求的区别
- HttpUrlConnection的GET请求和POST请求
- Get请求和Post请求的区别
- HTTP的GET请求和POST请求
- Get请求和POST请求的区别
- Get请求和Post请求的区别
- GET请求和POST请求的区别
- HTTP请求的GET和POST请求
- GET请求和POST请求的区别
- get请求和post请求的特点
- spring注解1之@RequestMapping
- Linux 五大初始化系统
- 经典公开课、好的学习网站
- SSH架构图
- JAVAScript中DOM与BOM的差异分析
- 简单的POST请求和get请求
- 关于cocoaPods安装与使用
- 【考试分析】模拟考试阶段行为规范
- leetcode 232. Implement Queue using Stacks
- ssh框架
- UGUI--背包系统之三--------Slot
- 用树莓派搭建你自己的Web服务器,以及一个可以外网访问的Blog
- STL unique函数
- 第八周拓展—小明借书