使用程序发送http请求

需要jar包:
commons-httpclient.jar
commons-codec.jar
commons-logging.jar(或许不需要)
或依赖:

<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

代码:

//先创建一个连接对象
HttpClient client = new HttpClient();
//创建请求方法
HttpMethod postMethod = new PostMethod("http://127.0.0.1:8080/Test-WebApp/test");
//请求参数对象
NameValuePair[] params = new NameValuePair[]{new NameValuePair("aaa", "111")};

//把请求参数设置到请求方法里
postMethod.setQueryString(params);
//执行请求
client.executeMethod(postMethod);

//获取请求相应
String str = postMethod.getResponseBodyAsString();

猜你喜欢

转载自blog.csdn.net/xuejianbest/article/details/84822381