package com.second;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.junit.Test;
import java.io.IOException;
/**
* Created by Administrator on 2017/8/10.
*/
public class HttpClientTest {
@Test
public void getUserByIdTest() throws IOException {
HttpClient httpClient = new HttpClient();
String url = "http://localhost:8881/DemoController/getUserById?id=1";
GetMethod getMethod = new GetMethod(url);
int status = httpClient.executeMethod(getMethod);
String body = getMethod.getResponseBodyAsString();
System.out.print("返回的状态码:"+status+"返回的结果是:"+body);
}
@Test
public void getUserById() throws IOException {
HttpClient httpClient = new HttpClient();
String url = "http://localhost:8881/DemoController/getUserById";
GetMethod getMethod = new GetMethod(url);
//两个参数的时候2 new两个对象
NameValuePair[] nameValuePairs = new NameValuePair[1];
NameValuePair nameValuePair = new NameValuePair();
nameValuePair.setName("id");
nameValuePair.setValue("1");
nameValuePairs[0] = nameValuePair;
// NameValuePair nameValuePair2 = new NameValuePair();
// nameValuePair2.setName("name");
// nameValuePair2.setValue("longteng");
// nameValuePairs[1] = nameValuePair2;
//设置编码格式
HttpClientParams clientParams = httpClient.getParams();
clientParams.setContentCharset("UTF-8");
//设置超时
HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();
params.setConnectionTimeout(5000);
params.setSoTimeout(1000*60);
getMethod.setQueryString(nameValuePairs);
int status = httpClient.executeMethod(getMethod);
String body = getMethod.getResponseBodyAsString();
System.out.print("返回状态码:"+status+";返回结果"+body);
}
}
//post接口
@Test
public void postMethod () throws IOException {
HttpClient httpClient = new HttpClient();
String url = "http://localhost:8881/DemoController/getUserByIdPost?id=1";
PostMethod postMethod = new PostMethod(url);
int status = httpClient.executeMethod(postMethod);
String body = postMethod.getResponseBodyAsString();
System.out.print("返回状态码是:"+status+";返回结果是:"+body);
}
//带body体的 rest风格接口 用setRequestBody
@Test
public void postMethod1 (){
HttpClient httpClient = new HttpClient();
String url = "http://localhost:8881/DemoController/modifyUserByIdBody/1";
String body = "{\"name\":\"longtest\",\"email\":\"long@1111.com\"}";
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestBody(body);
try {
int status = httpClient.executeMethod(postMethod);
String body1 = postMethod.getResponseBodyAsString();
System.out.print("返回状态码是:"+status+";返回结果是:"+body1);
} catch (IOException e) {
e.printStackTrace();
}
}
//授权 授权信息加在header里 用setRequestHeader
@Test
public void authorizationTest(){
HttpClient httpClient = new HttpClient();
String url = "http://localhost:8881/DemoController/getUserByIdAuth?id=1";
GetMethod getMethod = new GetMethod(url);
getMethod.setRequestHeader("Authorization","bG9uZ3Rlbmc6dGVzdA==");
try {
int status = httpClient.executeMethod(getMethod);
String body = getMethod.getResponseBodyAsString();
System.out.print("返回状态码:"+status+"返回结果是"+body);
} catch (IOException e) {
e.printStackTrace();
}
}
//带cookie的,登录请求,服务器把sessionid 放到cookie里
@Test
public void cookieTest() throws IOException {
HttpClient httpClient = new HttpClient();
String url1 = "http://localhost:8881/LoginController/login?password=a&username=a";
PostMethod postMethod = new PostMethod(url1);
int status1 = httpClient.executeMethod(postMethod);
String body1 = postMethod.getResponseBodyAsString();
System.out.print("返回状态码:"+status1+"返回结果"+body1);
Cookie[] cookies = httpClient.getState().getCookies();
for (Cookie c:cookies){
System.out.print("name"+c.getName()+",,,,,value"+c.getValue());
}
String url = "http://localhost:8881/DemoController/getUserByIdSession?id=1";
GetMethod getMethod = new GetMethod(url);
try {
int status = httpClient.executeMethod(getMethod);
String body = getMethod.getResponseBodyAsString();
System.out.print("返回状态码:"+status+";返回结果是"+body);
} catch (IOException e) {
e.printStackTrace();
}
}
//代理请求
@Test
public void proxyTest() throws IOException {
HttpClient httpClient = new HttpClient();
String url ="http:www.baidu.com";
httpClient.getHostConfiguration().setProxy("127.0.0.1",8881);
GetMethod getMethod = new GetMethod(url);
int status = httpClient.executeMethod(getMethod);
String body = getMethod.getResponseBodyAsString();
System.out.print("返回状态码:"+status+";返回结果是"+body);
}
//重定向
@Test
public void forwardTest() throws IOException {
HttpClient httpClient = new HttpClient();
String url = "http://localhost:8881/forWard.jsp?id=1";
GetMethod getMethod = new GetMethod(url);
//第一种方式自动跟随重定向一般是打开的true ,如果关闭返回状态码就是302
getMethod.setFollowRedirects(false);
int status = httpClient.executeMethod(getMethod);
String body = getMethod.getResponseBodyAsString();
System.out.print("返回状态码:"+status+";返回结果是"+body);
//第二种方式 header 这里的url1 是真正的请求地址
Header header = getMethod.getResponseHeader("location");
String url1 = header.getValue();
System.out.print(url1);
}