日期:2014-05-17  浏览次数:20870 次

jsp+httpclient4 cookie共享的问题
本帖最后由 icejelly_cn 于 2011-07-14 17:19:10 编辑
我想实现的功能很简单 以58网为例 单击我页面(Wuba.jsp)上面的按钮 页面转向58网的个人主页 当然这个主页没有登录账号是不能进的 所以我在控制层用httpclient登录了 如果浏览器和httpclient的cookie是共享的 应该就可以正常转向个人主页了 但实验结果却不是如此 本人菜鸟 向各位朋友讨教讨教 先谢了!

Wuba.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>测试</title>
  </head>
  
  <body>
  <form method="get" action="${pageContext.request.contextPath}/WubaServlet">
    <input type="submit" value="进入58个人主页">
  </form>
  </body>
</html>


WubaServlet.java

package cn.icejelly.httpclient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.protocol.HTTP;

public class WubaServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletExceptionIOException {

response.setContentType("text/html;charset=utf-8");
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://passport.58.com//login?path=http://my.58.com");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "majia123"));
params.add(new BasicNameValuePair("password", "majia123"));

try {
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block