如何在 Java 中发送 POST 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12429356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I send a POST request in Java?
提问by Qafqaz Qafqaz
When I send request from JSP I use this code
当我从 JSP 发送请求时,我使用此代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="http://translate.intelsoft.az" id="tform" name="ftext">
<input class="gogo1" value="a" name="l" id="l1" /> <div class="il">
<p>Rusca</p>
<textarea class="ilkin1" name="t" id="t1" >
выыававыавыавыавфыа
выыававыавыавыавфыа
выыававыавыавыавфыа
выыававыавыавыавфыа</textarea>
<div><input class="gogo" type="submit" value="T?rcüm?1" name="b1" /></div></div> </form>
</body>
</html>
and the response is correct, so that I see my parameter's value. But when I send from Java I get no correct response. I think that the parameters are not sent correctly. Here's my Java code:
并且响应是正确的,因此我可以看到我的参数值。但是当我从 Java 发送时,我没有得到正确的响应。我认为参数发送不正确。这是我的Java代码:
String urlParameters = "t=выыававыавыавыавфыа&l=a";
String request = "http://translate.intelsoft.az";
URL url = null;
try {
url = new URL(request);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
try {
connection.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.setRequestProperty("Content-Type", "text/html");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr;
try {
wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
connection.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
What is wrong here?
这里有什么问题?
回答by BalusC
First, your JSP page is using UTF-8 character encoding.
首先,您的 JSP 页面使用的是 UTF-8 字符编码。
<%@ page ... pageEncoding="UTF-8"%>
However, your Java code isn't using the same character encoding.
但是,您的 Java 代码未使用相同的字符编码。
DataOutputStream wr;
try {
wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters); // <--- Wrong! Uses platform default encoding.
wr.flush();
You need to replace that troublesome piece by
你需要把那个麻烦的部分替换成
try {
connection.getOutputStream().write(urlParameters.getBytes("UTF-8"));
Note that the whole DataOutputStream
decoration is unnecessary. It serves an entirely different purpose (namely writing of .dat
type files). Don't forget to specify the same charset in the way how you've set your Content-Length
header.
请注意,整个DataOutputStream
装饰是不必要的。它有一个完全不同的目的(即编写.dat
类型文件)。不要忘记以设置Content-Length
标题的方式指定相同的字符集。
Second, the parameter names/values themselves should be URL-encoded in order to be extracted properly form the HTTP request.
其次,参数名称/值本身应该是 URL 编码的,以便从 HTTP 请求中正确提取。
String urlParameters = "t=" + URLEncoder.encode("выыававыавыавыавфыа", "UTF-8")
+ "&l=" + URLEncoder.encode("a", "UTF-8");
Third, your request headers are actually also wrong:
第三,您的请求标头实际上也是错误的:
connection.setRequestProperty("Content-Type", "text/html");
connection.setRequestProperty("charset", "utf-8");
You aren't sending text/html
data at all. You are sending application/x-www-form-urlencoded
data. Also, that charset
should have been an attribute of the Content-Type
header, thus so:
您根本没有发送text/html
数据。您正在发送application/x-www-form-urlencoded
数据。此外,这charset
应该是Content-Type
标题的一个属性,因此:
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");