其中客户端类:
import java.io.IOException;
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.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class TestClient {
public static void main(String args[]) throws IOException{
TestClient.doGet();
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
TestClient.doPost();
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
TestClient.doDelete();
}
/**
* Get获取
* SongWei
*/
public static void doGet(){
String url = "http://localhost:8080/FrameProject/ReturnMessage";
//构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//创建GET方法的实例
GetMethod getMethod = new GetMethod(url);
//使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
//执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
//读取内容
byte[] responseBody = getMethod.getResponseBody();
//处理内容
System.out.println(new String(responseBody,"UTF-8"));
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
//发生网络异常
e.printStackTrace();
} finally {
//释放连接
getMethod.releaseConnection();
}
}
/**
* Post获取
* SongWei
*/
public static void doPost() throws IOException{
String url = "http://localhost:8080/FrameProject/ReturnMessage";
// url="http://www.newsmth.net/bbslogin2.php";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair("id", "youUserName"),
new NameValuePair("passwd", "yourPwd") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(statusCode);
//读取内容
byte[] responseBody = postMethod.getResponseBody();
//处理内容
System.out.println(new String(responseBody,"UTF-8"));
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
return;
}else{
System.out.println("other");
}
}
/**
* Delete获取
* SongWei
*/
public static void doDelete(){
String url = "http://localhost:8080/FrameProject/ReturnMessage";
//构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//创建GET方法的实例
DeleteMethod deleteMethod = new DeleteMethod(url);
//使用系统提供的默认的恢复策略
deleteMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
//执行getMethod
int statusCode = httpClient.executeMethod(deleteMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ deleteMethod.getStatusLine());
}
//读取内容
byte[] responseBody = deleteMethod.getResponseBody();
//处理内容
System.out.println(new String(responseBody,"UTF-8"));
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
//发生网络异常
e.printStackTrace();
} finally {
//释放连接
deleteMethod.releaseConnection();
}
}
}