繁体   English   中英

在Java中使用HTTP请求发送cookie

[英]Sending cookie with HTTP request in Java

我试图通过创建一系列Http请求来在Java客户端中获取某个cookie。 看起来我从服务器上获取了有效的cookie,但是当我向看似有效的cookie发送到最终URL的请求时,我应该在响应中获取一些XML行,但是响应为空,因为cookie是错误的或无效的,因为会话已关闭或其他无法解决的问题。 服务器分发的cookie在会话结束时到期。

在我看来,该cookie是有效的,因为当我在Firefox中进行相同的调用时,一个具有相同名称且以3个首字母相同且长度相同的开头的相似cookie被存储在firefox中,并且在会议。 然后,如果我仅使用存储在firefox中的特定cookie来请求最终URL(删除了所有其他cookie),则xml会很好地呈现在页面上。

关于这段代码我做错了什么的任何想法? 另一件事,当我在这段代码中使用在Firefox中生成并存储的非常相似的cookie中的值时,最后一个请求的确在HTTP响应中提供了XML反馈。

// Validate
        url = new URL(URL_VALIDATE);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Cookie", cookie);
        conn.connect();

        String headerName = null;
        for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
            if (headerName.equals("Set-Cookie")) {
                if (conn.getHeaderField(i).startsWith("JSESSIONID")) {
                    cookie = conn.getHeaderField(i).substring(0, conn.getHeaderField(i).indexOf(";")).trim();
                }
            }
        }

        // Get the XML
        url = new URL(URL_XML_TOTALS);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Cookie", cookie);
        conn.connect();

        // Get the response
        StringBuffer answer = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            answer.append(line);
        }
        reader.close();

        //Output the response
        System.out.println(answer.toString())

我有点懒得无法调试代码,但是您可以考虑让CookieHandler做繁重的工作。 这是我之前做的:

public class MyCookieHandler extends CookieHandler {
  private final Map<String, List<String>> cookies = 
                                            new HashMap<String, List<String>>();

  @Override public Map<String, List<String>> get(URI uri,
      Map<String, List<String>> requestHeaders) throws IOException {
    Map<String, List<String>> ret = new HashMap<String, List<String>>();
    synchronized (cookies) {
      List<String> store = cookies.get(uri.getHost());
      if (store != null) {
        store = Collections.unmodifiableList(store);
        ret.put("Cookie", store);
      }
    }
    return Collections.unmodifiableMap(ret);
  }

  @Override public void put(URI uri, Map<String, List<String>> responseHeaders)
      throws IOException {
    List<String> newCookies = responseHeaders.get("Set-Cookie");
    if (newCookies != null) {
      synchronized (cookies) {
        List<String> store = cookies.get(uri.getHost());
        if (store == null) {
          store = new ArrayList<String>();
          cookies.put(uri.getHost(), store);
        }
        store.addAll(newCookies);
      }
    }
  }
}

CookieHandler假定您的cookie处理对于JVM是全局的; 如果您希望按线程进行客户端会话或进行其他更复杂的事务处理,则最好还是坚持使用手动方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM