Java发送HTTP请求(GET和POST)
来源:互联网 发布:学建筑软件 编辑:程序博客网 时间:2024/03/29 13:02
HTTP请求工具类
import net.sf.json.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import java.io.IOException;public class HttpClientUtil { /** * 发送get请求 * @param url(get请求的url) * @return */ public static JSONObject doGetStr(String accessTokenUrl) { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(accessTokenUrl); JSONObject jsonObject = null; try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.fromObject(result); } httpGet.releaseConnection(); } catch (IOException e) { e.printStackTrace(); } return jsonObject; } /** * 发送post请求 * @param url * @param param * @return */ public static JSONObject doPostStr(String url,String param){ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); JSONObject jsonObject = null; try { httpPost.setEntity(new StringEntity(param, "UTF-8")); HttpResponse response = httpClient.execute(httpPost); String result = EntityUtils.toString(response.getEntity(),"UTF-8"); jsonObject = JSONObject.fromObject(result); } catch (IOException e) { e.printStackTrace(); } return jsonObject; }}
主要介绍一下post的param格式
JSONObject params = new JSONObject(); params.put("param1",param1); params.put("param2",param2); String param = params.toString();
阅读全文
0 0
- java发送http get 和 post 请求
- Java发送Http请求(Get和Post)
- Java发送HTTP请求(GET和POST)
- java发送HTTP请求 (get和post方法请求)
- JAVA 发送Http请求,GET、POST请求
- java 发送HTTP (get 和 post方法请求)
- Java发送HTTP的POST和GET方式请求
- java使用post和get方式发送http请求
- java之http发送post和get请求
- java发送HTTP GET POST请求
- java发送get、post http请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java 发送http请求post get
- 使用腾讯云 GPU 学习深度学习系列之一:传统机器学习的回顾
- Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Wi
- 扫雷程序(C语言并优化): 1>第一次下子,不炸死。 2>坐标周围没雷,可以实现展开。
- Android Studio 常见 Build 问题 Error:failed to find Build Tools revision 26.0.2
- 【原】react-native start EACCESS error for setup_env.sh
- Java发送HTTP请求(GET和POST)
- 简单选择排序验证性实验
- 《20171210》
- AngularJS 服务
- angular-route传递参数
- weide科技公司Java实习生面试总结
- 思科2960交换机密码破解
- 人生苦短,我用python
- 身份证号键盘输入限制开发总结