http请求一个servlet(接口)地址

以前测试一个自己写的接口总是用火狐浏览器的httpRequester进行请求,但是那个东西并不准确(有的时候能捕捉到没有被捕获的异常),而且后台上要跟其它的平台进行数据交互,想自己写一个http请求小方法然后进行测试,后台也能用上。MDZZ研究一上午,中午想明白了遂写代码测试成功,贴出来记录一下。

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSONObject;

public class HttpClientTest {

	public static void main(String[] args) throws HttpException, IOException {
		//请求地址
		String url="http://xxxx:xxxx/xxxx/xxxxServlet";
		//组装请求数据
		JSONObject json=new JSONObject();
		JSONObject requestJson=new JSONObject();
		json.put("A", "A");
		json.put("B", "B");
		requestJson.put("C", "C");
		requestJson.put("D", "D");
		json.put("request", requestJson);
		
		/******正式的数据请求******/
		HttpClient client=new HttpClient();
		PostMethod method=new PostMethod(url);
		byte[] bytes = json.toString().getBytes("UTF-8");
		InputStream inputStream = new ByteArrayInputStream(bytes,0,bytes.length);
		//setRequestBody方法在http3.1之后被替换成setRequestEntity
		RequestEntity re=new InputStreamRequestEntity(inputStream,bytes.length,"charset=utf-8");
		method.setRequestEntity(re);
		
		method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
		//设置超时的代码段
		HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
		//设置连接超时时间(单位毫秒)
		managerParams.setConnectionTimeout(10000);
		//查看状态
		int status = client.executeMethod(method);
		if (status != HttpStatus.SC_OK) {
			System.out.println( "连接" + url+ "出错!错误代码为:" + status);
			throw new IllegalStateException("Method failed: "+ method.getStatusLine());
		}
		
		//处理返回的数据
		InputStream txtis = method.getResponseBodyAsStream();
		/* eclipse 环境开发环境迁移之后导致的返回报文乱码问题修改 */
		BufferedReader br = new BufferedReader(new InputStreamReader(txtis,Charset.forName("UTF-8")));
		
		//为了更直观的看到返回的数据而写的方法
		StringBuffer html = new StringBuffer(100);
		String tempbf;
		while ((tempbf = br.readLine()) != null) {
			html.append(tempbf);
		}
		method.releaseConnection();
		
		System.out.println(html.toString());
	}
	
}


大致就是这样的请求方式,经测试暂无问题。还有一种接口的请求方式原理上大致就是在地址后面拼上参数然后整个长地址进行请求,只需把url后面拼完参数整个的地址请

求即可。上述方法如果有乱码的情况改改请求格式即可。













  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值