作者:lj劲-Ybw_582 | 来源:互联网 | 2023-05-19 06:56
我想做一个用java提交post请求的客户端应用。浏览器网址:http:www.pkuschool.edu.cnQingjiaQingjiaG.aspxpost提交地址:http:ww
我想做一个用java提交post请求的客户端应用。
浏览器网址:http://www.pkuschool.edu.cn/Qingjia/QingjiaG.aspx
post提交地址:http://www.pkuschool.edu.cn/Qingjia/Data/AjaxService.aspx?method=SaveQingjiaG
网站是用miniui做的,根据表单自动生成json数据,提交到服务器,返回一个json字符串。
我无法查看服务器端文件代码,但根据现有调试成果,服务器端先检测是否是json数据,每个jsonObject是否齐全,然后再检测每个json的值是否匹配。我模拟提交的json是齐全的,但服务端返回{"returnMessage":"信息提交异常,详细信息:输入字符串的格式不正确。","isSend":null},我认为是中文乱码问题。
我提交的数据是抓包后用UTF-8解码的数据,即:[{\"Nianji\":\"高中2015届\",\"Banji\":\"元培书院\",\"Xuehao\":\"1525998\",\"Xingming\":\"姓名\",\"JianhurenShouji\":\"11111111111\",\"QingjiaLeixing\":\"病假\",\"KaishiShijian\":\"2014-05-24T00:00:00\",\"KaishiJie\":\"第1节\",\"JieshuShijian\":\"2014-05-24T00:00:00\",\"JieshuJie\":\"第1节\",\"QingjiaYuanyin\":\"原因\",\"key\":\"\",\"pagesize\":5}]
客户端代码:
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class LeaveSetTest2 {
public static void main(String[] args) {
String data1 = "[{\"Nianji\":\"高中2015届\",\"Banji\":\"元培书院\",\"Xuehao\":\"1525998\",\"Xingming\":\"姓名\",\"JianhurenShouji\":\"11111111111\",\"QingjiaLeixing\":\"病假\",\"KaishiShijian\":\"2014-05-24T00:00:00\",\"KaishiJie\":\"第1节\",\"JieshuShijian\":\"2014-05-24T00:00:00\",\"JieshuJie\":\"第1节\",\"QingjiaYuanyin\":\"原因\",\"key\":\"\",\"pagesize\":5}]";
List list = new ArrayList();
HttpResponse response = null;
try {
data1 = new String(data1.getBytes("GBK"), "UTF-8");
list.add(new BasicNameValuePair("data", data1));
HttpParams params = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(params);
String uri = "http://www.pkuschool.edu.cn/Qingjia/Data/AjaxService.aspx?method=SaveQingjiaG";
HttpPost post = new HttpPost(uri);
post.setEntity(new UrlEncodedFormEntity(list));
response = client.execute(post);
System.out.println(EntityUtils.toString((response.getEntity())));
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
求大神能帮我调试一下。。。我把各种编码都试了。。。才疏学浅,实在无力解决了。。。
此网站没有登录什么的,完全就是一个独立的post,无COOKIE,session。。。
求帮忙!!
附带一些数据:
request:
(Request-Line) POST /Qingjia/Data/AjaxService.aspx?method=SaveQingjiaG HTTP/1.1
Host www.pkuschool.edu.cn
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept */*
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip, deflate
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://www.pkuschool.edu.cn/Qingjia/QingjiaG.aspx
Content-Length 598
Connection keep-alive
Pragma no-cache
Cache-Control no-cache
response:
(Status-Line) HTTP/1.1 200 OK
Cache-Control private
Date Thu, 07 Aug 2014 08:37:02 GMT
Content-Length 104
Content-Type text/html; charset=utf-8
Server IIS
X-Powered-By WAF/2.0
5 个解决方案
把这个注释掉data1 = new String(data1.getBytes("GBK"), "UTF-8");
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));