如何使用 HttpURLConnection 使 HTTP/1.1 POST 在 Java 中工作?
How do I get this HTTP/1.1 POST to work in Java using HttpURLConnection?
我正在尝试使用 HttpURLConnection
发送 HTTP/1.1 POST 到 http://uploaded.net/io/login,以便使用我的用户名和密码登录该网站。
由于某种原因,似乎 byte[] postData
没有被发送到服务器。
我收到响应代码和消息 200 OK,但服务器回复 JSON-formatted 错误消息 {"err": "Please type in id and password"} 就好像我从未真正发送过数据一样。
我尝试了不同的方法(发送 String
、byte[]
等,使用不同的输出编写器)发送数据,但到目前为止 none 已经奏效并且我不知道它到底在哪里失败了。
这是我正在使用的代码片段:
package post;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
public class PostTest {
public static void login() throws MalformedURLException, IOException {
String urlParameters = String.format("id=%s&password=%s",
URLEncoder.encode(Config.getProperty("account.1.username"), "UTF-8"),
URLEncoder.encode(Config.getProperty("account.1.password"), "UTF-8")
);
byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8"));
URL url = new URL(Config.getProperty("login.url"));
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setUseCaches(false);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Length",
Integer.toString(postData.length));
//prints: 26
System.out.println(postData.length);
//prints: id=test&password=123456789
System.out.println(urlParameters);
httpConn.getOutputStream().write(postData);
httpConn.getOutputStream().flush();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null;)
System.out.println(line);
}
}
}
Live HTTP Headers(Firefox 插件)输出:
POST /io/login HTTP/1.1
Host: uploaded.net
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/javascript, text/html, application/xml, text/xml, */*
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
X-Prototype-Version: 1.6.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://uploaded.net/
Content-Length: 20
Cookie: __utma=9[censored...]
Connection: keep-alive
id=test&pw=123456789
HTTP/1.1 200 OK
[...]
编辑: 添加了 Firefox' Live HTTP Headers 输出,我用它来查看正在发送的数据POST.
我假设 byte[] postData
发送不正确(如果有的话)是否正确?
我必须进行哪些更改才能使登录正常工作?
呃,我终于意识到我的错误了。因此,我尝试使用 localhost/test.php 进行首次登录,其中参数为 password=<pw>
我完全忽略了我将数据发送到的实际网站正在使用 pw=<pw>
。
将 String.format("id=%s&password=%s", [...]
更改为 String.format("id=%s&pw=%s", [...]
后,效果非常好。
我正在尝试使用 HttpURLConnection
发送 HTTP/1.1 POST 到 http://uploaded.net/io/login,以便使用我的用户名和密码登录该网站。
由于某种原因,似乎 byte[] postData
没有被发送到服务器。
我收到响应代码和消息 200 OK,但服务器回复 JSON-formatted 错误消息 {"err": "Please type in id and password"} 就好像我从未真正发送过数据一样。
我尝试了不同的方法(发送 String
、byte[]
等,使用不同的输出编写器)发送数据,但到目前为止 none 已经奏效并且我不知道它到底在哪里失败了。
这是我正在使用的代码片段:
package post;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
public class PostTest {
public static void login() throws MalformedURLException, IOException {
String urlParameters = String.format("id=%s&password=%s",
URLEncoder.encode(Config.getProperty("account.1.username"), "UTF-8"),
URLEncoder.encode(Config.getProperty("account.1.password"), "UTF-8")
);
byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8"));
URL url = new URL(Config.getProperty("login.url"));
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setUseCaches(false);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Length",
Integer.toString(postData.length));
//prints: 26
System.out.println(postData.length);
//prints: id=test&password=123456789
System.out.println(urlParameters);
httpConn.getOutputStream().write(postData);
httpConn.getOutputStream().flush();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null;)
System.out.println(line);
}
}
}
Live HTTP Headers(Firefox 插件)输出:
POST /io/login HTTP/1.1
Host: uploaded.net
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/javascript, text/html, application/xml, text/xml, */*
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
X-Prototype-Version: 1.6.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://uploaded.net/
Content-Length: 20
Cookie: __utma=9[censored...]
Connection: keep-alive
id=test&pw=123456789
HTTP/1.1 200 OK
[...]
编辑: 添加了 Firefox' Live HTTP Headers 输出,我用它来查看正在发送的数据POST.
我假设 byte[] postData
发送不正确(如果有的话)是否正确?
我必须进行哪些更改才能使登录正常工作?
呃,我终于意识到我的错误了。因此,我尝试使用 localhost/test.php 进行首次登录,其中参数为 password=<pw>
我完全忽略了我将数据发送到的实际网站正在使用 pw=<pw>
。
将 String.format("id=%s&password=%s", [...]
更改为 String.format("id=%s&pw=%s", [...]
后,效果非常好。