package pro;
import java.io.IOException;
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.PostMethod;
public class simulateWebAction {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String url = “*******”;
PostMethod postMethod = new UTF8PostMethod(url);
StringBuilder origin = new StringBuilder();
origin.setLength(0);
HttpClient httpClient = new HttpClient();
// getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
NameValuePair a = new NameValuePair("a","**");
NameValuePair q = new NameValuePair("q","**");
NameValuePair[] param = new NameValuePair[]{a,q};
postMethod.setRequestBody(param);
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "+ postMethod.getStatusLine());
}else{
// 读内容
System.out.println(postMethod.getResponseBodyAsString());
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
postMethod.releaseConnection();
}
}
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "UTF-8";
}
}
}