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";
}
}
}
httpclient post数据时,中文乱码问题
最新推荐文章于 2024-02-18 14:44:39 发布