JAVA发送HTTP请求
来源:互联网 发布:2017年淘宝双12新玩法 编辑:程序博客网 时间:2024/06/06 07:32
package oa.api.all;import java.util.*;import org.apache.http.*;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.*;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.json.*;@SuppressWarnings("deprecation")public class Request { private String result; private HttpClient http_client = new DefaultHttpClient(); /*发送get请求 * @param request_url请求url * @param request_header请求头(map) * */ public String set_get_request(String request_url, Map<String, String> request_header){ try{ HttpGet get_request = new HttpGet(request_url); //发送请求头 Set<String> set = request_header.keySet(); Iterator<String> iter = set.iterator(); while (iter.hasNext()) { String key = iter.next().toString(); String value = request_header.get(key); get_request.setHeader(key,value); } //获取请求响应 HttpResponse get_response = http_client.execute(get_request); HttpEntity get_entity = get_response.getEntity(); result = EntityUtils.toString(get_entity); } catch(Exception e){ System.out.println(e); } return result; } /*发送post请求 * 请求体有四种格式 * @param request_url 请求url * @param request_header请求头(map) * @param request_body 为json格式的字符串数据 * @param type,请求类型,1:json;2:form-data * */ public String set_post_request(String request_url, Map<String, String> request_header, String request_body, int type){ try{ HttpPost post_request = new HttpPost(request_url); //发送请求头 Set<String> set = request_header.keySet(); Iterator<String> iter = set.iterator(); while (iter.hasNext()) { String key = iter.next().toString(); String value = request_header.get(key); post_request.setHeader(key,value); } //发送请求体 JSONObject json_obj = new JSONObject(request_body); System.out.println(json_obj); //发送 json 格式的请求体 if(type==1){ StringEntity s = new StringEntity(json_obj.toString()); post_request.setEntity(s); } //发送form-data格式的请求体 else if(type==2){ List<NameValuePair> form_params = new ArrayList<NameValuePair>(); //List formParams = new ArrayList(); Iterator<String> it = json_obj.keys(); // 遍历jsonObject数据,添加到Map对象 while (it.hasNext()) { String key = String.valueOf(it.next()); String value = (String) json_obj.get(key); form_params.add(new BasicNameValuePair(key, value)); } HttpEntity entity = new UrlEncodedFormEntity(form_params, "UTF-8"); post_request.setEntity(entity); } //获取请求响应 HttpResponse get_response = http_client.execute(post_request); HttpEntity get_entity = get_response.getEntity(); result = EntityUtils.toString(get_entity); } catch(Exception e){ System.out.println(e); } return result; }}
2 0
- java 发送http请求
- JAVA发送HTTP请求
- Java发送Http请求
- java 发送http请求
- Java发送http请求
- java发送http请求
- java发送http请求
- java发送http请求
- java发送http请求
- java 发送http请求
- java发送http请求
- java发送http请求
- Java发送HTTP请求
- java发送http请求
- java发送http请求
- java发送http请求
- Java发送http请求
- java 发送http请求
- 盘点Linux十大版本
- 动态规划-美团笔试-最大直方图
- Linux下安装redis
- maven 添加jar包
- 继承中的构造方法
- JAVA发送HTTP请求
- kafka基本使用实战
- Mysql 5.6 ONLINE_DDL
- 学思自动套料软件
- 2016-4-11华为技术免试和综合面试
- upper_bound()与lower_bound()使用方法
- 一道关于OO面试题
- 用cmd内置telnet客户端模拟http请求
- C#中怎么定义不定长数组