Java 发送post数据时HttpClient自动注销
我有一个sendPost()方法,它发送一个post数据以登录到某个站点。我能得到302的应答码。执行此方法后,我有一个sendPost2()方法,如果我成功登录,该方法将起作用。但是,我在sendPost2()中得到了200的响应代码,它还告诉我没有登录。似乎在执行sendPost()之后,httpclient会将我注销。如何防止它注销 这是我的sendPost(),但我无法为您提供有效的用户名和密码:Java 发送post数据时HttpClient自动注销,java,apache,httpclient,Java,Apache,Httpclient,我有一个sendPost()方法,它发送一个post数据以登录到某个站点。我能得到302的应答码。执行此方法后,我有一个sendPost2()方法,如果我成功登录,该方法将起作用。但是,我在sendPost2()中得到了200的响应代码,它还告诉我没有登录。似乎在执行sendPost()之后,httpclient会将我注销。如何防止它注销 这是我的sendPost(),但我无法为您提供有效的用户名和密码: private void sendPost() throws Exception {
private void sendPost() throws Exception {
String url = "http://sblive.auf.edu.ph/schoolbliz/commfile/login.jsp";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// add header
post.setHeader("User-Agent", USER_AGENT);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("user_id", "testusername"));
urlParameters.add(new BasicNameValuePair("password", "testpassword"));
urlParameters.add(new BasicNameValuePair("x", "47"));
urlParameters.add(new BasicNameValuePair("y", "1"));
urlParameters.add(new BasicNameValuePair("body_color", "#9FBFD0"));
urlParameters.add(new BasicNameValuePair("welcome_url", "../PARENTS_STUDENTS/main_files/login_success.htm"));
urlParameters.add(new BasicNameValuePair("login_type", "parent_student"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
private void sendPost()引发异常{
字符串url=”http://sblive.auf.edu.ph/schoolbliz/commfile/login.jsp";
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(url);
//添加标题
post.setHeader(“用户代理”,用户代理);
List urlParameters=new ArrayList();
添加(新的BasicNameValuePair(“用户id”、“测试用户名”);
添加(新的BasicNameValuePair(“密码”、“测试密码”);
添加(新的BasicNameValuePair(“x”,“47”));
添加(新的BasicNameValuePair(“y”,“1”));
添加(新的BasicNameValuePair(“body#color”,“9FBFD0”);
添加(新的BasicNameValuePair(“欢迎”、“./PARENTS\u STUDENTS/main\u files/login\u success.htm”);
添加(新的BasicNameValuePair(“登录类型”、“家长学生”);
setEntity(新的UrlEncodedFormEntity(urlParameters));
HttpResponse response=client.execute(post);
System.out.println(“\n向URL发送'POST'请求:“+URL”);
System.out.println(“Post参数:+Post.getEntity());
System.out.println(“响应代码:”+
response.getStatusLine().getStatusCode());
BufferedReader rd=新的BufferedReader(
新的InputStreamReader(response.getEntity().getContent());
StringBuffer结果=新的StringBuffer();
字符串行=”;
而((line=rd.readLine())!=null){
结果。追加(行);
}
System.out.println(result.toString());
配方
- 准备
- 把它放在盒子里
- 将上下文传递给每个调用
当与网站交互时,因为HTTP本身是无状态协议,网站通常使用Cookie来存储会话数据。从您的代码中,我看不到您正在发送网站希望您发送的Cookie(关闭Cookie的浏览器行为)。由于您发送的每个请求都是空白Cookie,因此自最初的POST响应以来,网站假定您是一个完整的独立客户。基本上,您必须重新发送网站希望您存储的所有Cookie。
HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// ...
HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...
HttpResponse response2 = httpClient.execute(method2, httpContext);
// ...