I got a strange problem. I have code that makes a call on the server. The code works fine when I use the Kitkat emulator. However, the same code does not work on Gingerbread phones or the emulator. I always get 400 Bad Request from the server. I checked on the server. Error:
the client sent an HTTP / 1.1 request without a host name (see RFC2616 section 14.23):
Apache is not configured as a virtual host.
here is the code i use:
public void makeServerCall (String serverUrl, String postString) {URL url = new URL (serverURL + ESS_AUTHENTICATE_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(HTTP_POST);
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:1.3.1)");
String base64Tenant = "Basic " + Base64.encodeToString( auth.getBytes(), Base64.DEFAULT);
connection.setRequestProperty(AUTHENTICATE_HEADER, base64Tenant);
connection.addRequestProperty("Cache-Control", "no-cache");
connection.addRequestProperty("Pragma", "no-cache");
connection.setUseCaches(false);
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept-Charset", "UTF-8");
System.setProperty("http.keepAlive", "false");
connection.setFixedLengthStreamingMode(postString.getBytes().length);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print(postString);
out.close();
int respCode = connection.getResponseCode();
Log.i(TAG, "Response code = " + respCode);
if (respCode != 200) {
is = connection.getErrorStream();
if (is != null) {
String response = getStringFromStream(is);
Log.d(TAG, "Error occured: " + response);
is.close();
}
}
else {
}
}
Here is the wirehark capture I got when I run the code on the Kitkat POST / removed_path HTTP / 1.1 simulator
Accept-Charset: UTF-8
User-Agent: Mozilla/5.0 (Macintosh; U; PPC; en-US; rv: 1.3.1)
: Basic ASVGQVVMVEASDFw ==
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/x-www-form-urlencoded
Host: correcthost.myserver.com
: Keep-Alive
Accept-Encoding: gzip
Content-Length: 49
postHTTP/1.1 200 OK
: , 04 2014 07:52:42 GMT
wirehark Gingerbread
POST/deleted_path HTTP/1.1
Accept-Charset: UTF-8
User-Agent: Mozilla/5.0 (Macintosh; U; PPC; en-US; rv: 1.3.1)
: Basic ASVGQVVMVEASDFw ==
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/x-www-form-urlencoded
Content-Length: 49
Host: correcthost.myserver.com
: Keep-Alive
Accept-Encoding: gzip
- .
. , - , Android iOS. , ?