Alb*_*rto 11 java cookies post httpurlconnection
我正在尝试发送带有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();
// Send the request to the server
//conn.connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
问题是请求是在没有cookie的情况下发送的.如果我只做:conn.connect(); 并且不发送数据,cookie被发送好了.我无法确切地检查发生了什么,因为连接是SSL.我只检查回复.
根据URLConnectionjavadoc:
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
Run Code Online (Sandbox Code Playgroud)
您是否已确认在您的测试用例中,请求是否已进入服务器?我看你有没有调用connect()后getOutputStream()和注释掉之外.如果您取消注释并在通话前向上移动会发生什么getOutputStream()?
| 归档时间: |
|
| 查看次数: |
26209 次 |
| 最近记录: |