Reputation: 62
I'm trying to POST to a server and am getting HTTP 500 errors when getting the response. I've checked to make sure my params are correctly formatted. If anyone could tell me why my code isn't working I would greatly appreciate it!
String params = URLEncoder.encode(query, "UTF-8");
postConnection.setDoOutput(true);
postConnection.setRequestProperty("Accept-Charset", "UTF-8");
OutputStream output = postConnection.getOutputStream();
output.write(params.getBytes("UTF-8"));
output.flush();
output.close();
InputStream postResponse = postConnection.getInputStream(); //error happens here
int inputByte = postResponse.read();
while (inputByte != -1){
postResponseRawJSON += (char)inputByte;
inputByte = postResponse.read();
}
Upvotes: 0
Views: 544
Reputation: 32831
Don't urlencode your query string.
UPDATE
In a POST message, the body of the request is of the form:
name1=value1&name2=value2...
if you encode the whole query line, the '='s and '&'s will be escaped and the server will not be able to retrieve the parameters.
Some of your parameter values may need to be encoded; but you must encode them separately:
String params = "param1=" + URLEncoder.encode(value1, "UTF-8")
+ "¶m2=" + URLEncoder.encode(value2, "UTF-8")
...
Upvotes: 1