package com.baby.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class TestHttpClientPost {
HttpClient httpClient = new HttpClient();
String catId = "32";
public void post() throws HttpException, IOException{
String url = "http://localhost/phpcms/admin.php?mod=phpcms&file=content&action=add&catid=" + catId +"&modelid=1";
// httpClient.getHostConfiguration().setHost("localhost", 80, "http");
PostMethod postMethod = new UTF8PostMethod(url);
postMethod.setRequestHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
postMethod.setRequestHeader("connection", "Keep-Alive");
postMethod.setRequestHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4");
postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
postMethod.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=gbk");
postMethod.setRequestHeader("Cookie", "guestbook=3; uuid=3E7AFDB3DAC6AF5601A9969B2C78441EACC2B5C5E2E5B301; PHPSESSID=dee161c5bb102a2f7b0137141ee53d9f; EUrIWifQukusername=phpcms; EUrIWifQukauth=AT8HVwFWAQMOAAUGWlcHWFAEWlYAUFBTAFRQB1dSAFYFUg%3D%3D; EUrIWifQukcookietime=0");
String aaa = "info[catid]=[ ID]&info[title]=[ : ]&info[keywords]=[ : ]&info[author]=[ : ]" +
"&info[copyfrom]=[ : ]&add_introduce=1&introcude_length=200&auto_thumb=1" +
"&auto_thumb_no=1&info[content]=[ : ]&info[paginationtype]=0&info[maxcharperpage]=10000" +
"&status=99&info[inputtime]=[ : ]&info[posids]=-99]&info[posids][]=1&info[posids][]=2&info[posids][]=3&info[posids][]=4&info[posids][]=5" +
"&info[groupids_view]=-99&info[template]=show&dosubmit=1";
NameValuePair[] data = {
// id
new NameValuePair("info[catid]",catId),
new NameValuePair("info[title]"," ---"),
new NameValuePair("info[keywords]"," "),
new NameValuePair("info[author]","sina"),
//
new NameValuePair("info[copyfrom]"," "),
// X ( description) ,1 ,0
new NameValuePair("add_introduce","1"),
// , 200
new NameValuePair("introcude_length","200"),
// X , ,1 ,0
new NameValuePair("auto_thumb","1"),
// , ,
new NameValuePair("auto_thumb_no","1"),
new NameValuePair("info[content]","<center><img alt= src=http://i1.sinaimg.cn/ty/j/2009-11-06/U2463P6T12D4683237F44DT20091106093751.jpg border=1 ><br><img src=http://i0.sinaimg.cn/home/c.gif height=5 width=1 style=\"border:none;\"><br> : <br><br></center>" +
"<p> “ ” , 。 , , 。</p>" +
"<p> , , 。 , , 、 , 。 , 。 , 。 , , 。</p>"),
// , ,0 ,1
new NameValuePair("info[paginationtype]","0"),
// ,
new NameValuePair("info[maxcharperpage]","10000"),
//99
new NameValuePair("status","99"),
//
new NameValuePair("info[inputtime]","2009-11-24 12:53"),
// ,-99 , ,
new NameValuePair("info[posids]","-99"),
//1: 2: 3: 4: 5:
new NameValuePair("info[posids][]","1"),
new NameValuePair("info[posids][]","2"),
new NameValuePair("info[posids][]","3"),
new NameValuePair("info[posids][]","4"),
new NameValuePair("info[posids][]","5"),
// ,-99
new NameValuePair("info[groupids_view]","-99"),
//
new NameValuePair("info[template]","show"),
//
new NameValuePair("dosubmit","1")
};
postMethod.setRequestBody(data);
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(statusCode);
//
byte[] responseBody = postMethod.getResponseBody();
//
System.out.println(new String(responseBody,"UTF-8"));
// System.out.println(new String(postMethod.getResponseBodyAsString().getBytes("ISO-8859-1"),"GB2312"));
}
public static void main(String[] args) throws HttpException, IOException {
TestHttpClientPost t = new TestHttpClientPost();
t.post();
}
//Inner class for UTF-8 support
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "gbk";
}
}
}