HttpClient模拟get,post请求并发送请求参数(json等)
来源:互联网 发布:matlab cell数组相加 编辑:程序博客网 时间:2024/05/06 22:14
import java.io.IOException;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;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;/** * */public class HttpClientUtil {public static void main(String arg[]) throws Exception {String url = "test.com";JSONObject params = new JSONObject();params.put("SRC_STM_CODE", "wsf");params.put("TENANT_ID", "123");params.put("NM", "张三");params.put("BRTH_DT", "1983-01-20");params.put("GND_CODE", "1");JSONArray params2 = new JSONArray();JSONObject param3 = new JSONObject();param3.put("DOC_TP_CODE", "10100");param3.put("DOC_NBR", "100200198301202210");param3.put("DOC_CUST_NM", "test");params2.add(param3);params.put("DOCS", params2);String ret = doPost(url, params).toString();System.out.println(ret);}/** httpClient的get请求方式2 * @return * @throws Exception */public static String doGet(String url, String charset)throws Exception {/* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。 * 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get * 方法。 4:处理响应状态码。 5:若响应正常,处理 HTTP 响应内容。 6:释放连接。 *//* 1 生成 HttpClinet 对象并设置参数 */HttpClient httpClient = new HttpClient();// 设置 Http 连接超时为5秒httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);/* 2 生成 GetMethod 对象并设置参数 */GetMethod getMethod = new GetMethod(url);// 设置 get 请求超时为 5 秒getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);// 设置请求重试处理,用的是默认的重试处理:请求三次getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());String response = "";/* 3 执行 HTTP GET 请求 */try {int statusCode = httpClient.executeMethod(getMethod);/* 4 判断访问的状态码 */if (statusCode != HttpStatus.SC_OK) {System.err.println("请求出错: "+ getMethod.getStatusLine());}/* 5 处理 HTTP 响应内容 */// HTTP响应头部信息,这里简单打印Header[] headers = getMethod.getResponseHeaders();for (Header h : headers)System.out.println(h.getName() + "------------ " + h.getValue());// 读取 HTTP 响应内容,这里简单打印网页内容byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组response = new String(responseBody, charset);System.out.println("----------response:" + response);// 读取为 InputStream,在网页内容数据量大时候推荐使用// InputStream response = getMethod.getResponseBodyAsStream();} catch (HttpException e) {// 发生致命的异常,可能是协议不对或者返回的内容有问题System.out.println("请检查输入的URL!");e.printStackTrace();} catch (IOException e) {// 发生网络异常System.out.println("发生网络异常!");e.printStackTrace();} finally {/* 6 .释放连接 */getMethod.releaseConnection();}return response;}/** * post请求 * @param url * @param json * @return */public static JSONObject doPost(String url,JSONObject json){DefaultHttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(url);JSONObject response = null;try {StringEntity s = new StringEntity(json.toString());s.setContentEncoding("UTF-8");s.setContentType("application/json");//发送json数据需要设置contentTypepost.setEntity(s);HttpResponse res = client.execute(post);if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){HttpEntity entity = res.getEntity();String result = EntityUtils.toString(res.getEntity());// 返回json格式:response = JSONObject.fromObject(result);}} catch (Exception e) {throw new RuntimeException(e);}return response;}}
0 0
- HttpClient模拟get,post请求并发送请求参数(json等)
- HttpClient模拟get,post请求并发送请求参数(json等)
- HttpClient模拟get,post请求并发送请求参数(json等)
- HTTPCLIENT POST GET请求模拟
- http请求远程接口获得Json数据(2) --- 利用HttpClient发送post/get请求
- java使用HttpClient模拟发送http的get、post请求
- java httpclient POST和GET请求 发送JSON
- 【实战】(二)android模拟qq登录(apache的httpClient发送get和post请求)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- java httpclient发送post get 请求
- HttpClient发送get和post请求
- 使用Apache HttpClient发送get、post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- HttpClient发送Get、Post请求简单实践
- attempt to create delete event with null entity
- http讲解
- HDFS读写数据流程
- java线程并发之售票问题(上)
- 分布式搜索elasticsearch java API 之(三)(四)------索引数据、删除索引数据
- HttpClient模拟get,post请求并发送请求参数(json等)
- ab服务器暴力测试
- gcc编译链接多文件
- findbugs工具常见问题(转)
- 服务器外网端口无法访问解决办法
- linux下postgresql新增指定用户数据库
- 技术博客
- 路法Atiti
- 虚拟机下给Ubuntu挂载新硬盘