【发布时间】:2017-03-27 06:32:37
【问题描述】:
如何在 java 中使用x-www-form-urlencoded header 发送请求。我不明白如何发送带有键值的正文,如上面的屏幕截图所示。
我试过这段代码:
String urlParameters =
cafedra_name+ data_to_send;
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
但在响应中,我没有收到正确的数据。
【问题讨论】:
-
您是否捕获了使用 Wireshark 之类的工具发送的内容以确认该标头实际上不存在?
标签: java