Java HttpURLConnection - POST用Cookie [英] Java HttpURLConnection - POST with Cookie

查看:856
本文介绍了Java HttpURLConnection - POST用Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送带有Cookie的信息。这是代码:

  try {

String query = URLEncoder.encode(key UTF-8)+=+ URLEncoder.encode(value,UTF-8);
String cookies =session_cookie = value;
URL url = new URL(https:// myweb);
HttpsURLConnection conn =(HttpsURLConnection)url.openConnection();

conn.setRequestProperty(Cookie,cookies);
conn.setRequestMethod(POST);
conn.setDoInput(true);
conn.setDoOutput(true);

DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(query);
out.flush();
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String decodedString;
while((decodedString = in.readLine())!= null){
System.out.println(decodedString);
}
in.close();

//将请求发送到服务器
//conn.connect();
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

问题是请求没有发送cookie。如果我只做:
conn.connect();并且不发送数据,Cookie被发送确定。
我不能检查发生了什么,因为连接是thorugh SSL。根据 URLConnection javadoc:

 在连接远程对象之后,下列方法用于访问头字段和
内容:

* getContent
* getHeaderField
* getInputStream
* getOutputStream

您确认在上面的测试用例中,请求到达服务器吗?我看到你有 connect()之后 getOutputStream()和注释除了。如果您取消注释并在 getOutputStream()之前向上移动,会发生什么?


I´m trying to send a post request with cookies. This is the code:

try {

    String query = URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode("value", "UTF-8");
    String cookies = "session_cookie=value";
    URL url = new URL("https://myweb");
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

    conn.setRequestProperty("Cookie", cookies);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
    conn.setDoOutput(true);

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.writeBytes(query);
    out.flush();
    out.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String decodedString;
    while ((decodedString = in.readLine()) != null) {
        System.out.println(decodedString);
    }
    in.close();

    // Send the request to the server
    //conn.connect();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

The problem is the request is sent without the cookies. If I only make: conn.connect(); and don´t send data, the cookies are sent OK. I can´t check exactly what is happening, because the connection is thorugh SSL. I only check the response.

解决方案

According to the URLConnection javadoc:

The following methods are used to access the header fields and the 
contents AFTER the connection is made to the remote object:

* getContent
* getHeaderField
* getInputStream
* getOutputStream

Have you confirmed that in your test case above the request is getting to the server at all? I see you have the call to connect() after getOutputStream() and commented-out besides. What happens if you uncomment it and move up before the call to getOutputStream() ?

这篇关于Java HttpURLConnection - POST用Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆