我正在使用HttpURLConnection在Java中开发一个应用程序,首先,将POST发送到一个URL并获取该站点返回的令牌。然后,我使用此令牌在同一个站点中进行另一个POST,但在另一个URL中。当我这样做时,它会返回状态200 ,但在响应代码中写着“您使用的是过时的浏览器。请更新您的浏览器”。 我更改了用户代理,但仍然无效。
第一次请求:
String parameters="parameter=example1";
URL url = new URL("http://example1");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", parameters + Integer.toString(parameters.getBytes().length));
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.setRequestProperty("User-Agent","Mozilla/5.0 ");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
第二次请求:
String parameters1="paramters=example2&token="+token;
URL url1 = new URL("https://example1/form");
HttpURLConnection conn1 = (HttpURLConnection)url1.openConnection();
conn1.setRequestMethod("POST");
conn1.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn1.setRequestProperty("Content-Length", parameters1 + Integer.toString(parameters1.getBytes().length));
conn1.setRequestProperty("Content-Language", "en-US");
conn1.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn1.setRequestProperty("User-Agent","Mozilla/5.0");
来自第二个请求的标头响应
Status 200
Server: Apache-Example/1.1
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Cteonnt-Length: 6923
Cneonction: close
Date: Sat, 27 Jan 2018 13:23:53 GMT
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: JSESSIONID=example; Path=/;
X-Android-Sent-Millis: 1517059396002
X-Android-Received-Millis: 1517059396187
X-Android-Response-Source: NETWORK 200