When I send request from JSP I use this code
<%@ 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:
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?
First, your JSP page is using UTF-8 character encoding.
However, your Java code isn’t using the same character encoding.
You need to replace that troublesome piece by
Note that the whole
DataOutputStreamdecoration is unnecessary. It serves an entirely different purpose (namely writing of.dattype files). Don’t forget to specify the same charset in the way how you’ve set yourContent-Lengthheader.Second, the parameter names/values themselves should be URL-encoded in order to be extracted properly form the HTTP request.
Third, your request headers are actually also wrong:
You aren’t sending
text/htmldata at all. You are sendingapplication/x-www-form-urlencodeddata. Also, thatcharsetshould have been an attribute of theContent-Typeheader, thus so:See also: