/**
*
*/
package com.byzk.p2p.thread;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
/*** <p>Title:HttpClientTest </p>
* <p>Description:使用httpClient 包 实现post get 提交 </p>
* <p>Company:xbd </p>
* @author:Administrator
* @date:2016年11月7日上午11:53:10 */
public class HttpClientTest {
/**
*
* <p>Description:get 请求方式连接 </p>
* <p>Company:xbd </p>
* @author:lilei
* @date:2016年11月7日下午12:31:48
*/
public void GetTest(String url) {
HttpClient httpclient = new HttpClient();
// 创建get实例
GetMethod getmethod = new GetMethod(url);
getmethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statucode = httpclient.executeMethod(getmethod);
if (statucode != HttpStatus.SC_OK) {
System.err.println("method failed" + getmethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getmethod.getResponseBody();
// 处理内容
System.out.println(new String(responseBody));
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
getmethod.releaseConnection();
}
}
public void postTest(String url) {
HttpClient httpclient = new HttpClient();
//初始化post
PostMethod postmethod = new PostMethod(url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair pair = new NameValuePair("user_id", "1213");
list.add(pair);
postmethod.setRequestBody((NameValuePair[]) list.toArray());
//执行postmethod
try {
int statuCode = httpclient.executeMethod(postmethod);
if(statuCode == HttpStatus.SC_MOVED_PERMANENTLY || statuCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header header = postmethod.getRequestHeader("location");
String location="";
if(header!=null){
location = header.getValue();
System.out.println("the page was redirect to "+ location);
}else{
System.out.println("location value is null");
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*
*/
package com.byzk.p2p.thread;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
/*** <p>Title:HttpClientTest </p>
* <p>Description:使用httpClient 包 实现post get 提交 </p>
* <p>Company:xbd </p>
* @author:Administrator
* @date:2016年11月7日上午11:53:10 */
public class HttpClientTest {
/**
*
* <p>Description:get 请求方式连接 </p>
* <p>Company:xbd </p>
* @author:lilei
* @date:2016年11月7日下午12:31:48
*/
public void GetTest(String url) {
HttpClient httpclient = new HttpClient();
// 创建get实例
GetMethod getmethod = new GetMethod(url);
getmethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statucode = httpclient.executeMethod(getmethod);
if (statucode != HttpStatus.SC_OK) {
System.err.println("method failed" + getmethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getmethod.getResponseBody();
// 处理内容
System.out.println(new String(responseBody));
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
getmethod.releaseConnection();
}
}
public void postTest(String url) {
HttpClient httpclient = new HttpClient();
//初始化post
PostMethod postmethod = new PostMethod(url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair pair = new NameValuePair("user_id", "1213");
list.add(pair);
postmethod.setRequestBody((NameValuePair[]) list.toArray());
//执行postmethod
try {
int statuCode = httpclient.executeMethod(postmethod);
if(statuCode == HttpStatus.SC_MOVED_PERMANENTLY || statuCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header header = postmethod.getRequestHeader("location");
String location="";
if(header!=null){
location = header.getValue();
System.out.println("the page was redirect to "+ location);
}else{
System.out.println("location value is null");
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}