如何用Java发送POST请求?

时间:2012-09-14 17:36:16

标签: java jsp httprequest

当我从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> 

并且响应正确,因此我看到了参数的值。但是当我从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();
}

这里有什么问题?

1 个答案:

答案 0 :(得分:7)

首先,您的JSP页面使用UTF-8字符编码。

<%@ page ... pageEncoding="UTF-8"%>

但是,您的Java代码并没有使用相同的字符编码。

DataOutputStream wr;
try {
    wr = new DataOutputStream(connection.getOutputStream ());
    wr.writeBytes(urlParameters); // <--- Wrong! Uses platform default encoding.
    wr.flush();

您需要通过

替换那个麻烦的作品
try {
    connection.getOutputStream().write(urlParameters.getBytes("UTF-8"));

请注意,整个DataOutputStream装饰是不必要的。它有一个完全不同的目的(即编写.dat类型文件)。不要忘记为您设置Content-Length标题的方式指定相同的字符集。


其次,参数名称/值本身应该是URL编码的,以便从HTTP请求中正确提取。

String urlParameters = "t=" + URLEncoder.encode("выыававыавыавыавфыа", "UTF-8")
                     + "&l=" + URLEncoder.encode("a", "UTF-8");

第三,您的请求标题实际上也是错误的:

connection.setRequestProperty("Content-Type", "text/html"); 
connection.setRequestProperty("charset", "utf-8");

您根本不发送text/html数据。您正在发送application/x-www-form-urlencoded个数据。此外,charset应该是Content-Type标头的属性,因此:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

另见: