Saturday, April 12, 2014

http request using GZIP, but unsuccessful, Jiqiu you ~ ~! !


Without GZIP, no problem at all, using GZIP on the problem, described as follows:
Click the button to send the data to the server, then the server receives the data returned
Click the button every time the server receives the data failed, a successful one failed, very depressed! Engage in a day, and ask for help ah ~ ~

Android side code:
 public static String send (String url, String content, String contentType) throws Exception {
/ / Create connection
. HttpURLConnection conn = (HttpURLConnection) new URL (url) openConnection ();
conn.setDoInput (true);
conn.setDoOutput (true);
conn.setUseCaches (false);
if (contentType! = null) {
conn.setRequestProperty ("content-type", contentType);
} Else {
conn.setRequestProperty ("content-type", CONTENT_TYPE_HTML);
}
conn.setConnectTimeout (10000);
conn.setReadTimeout (60000);
conn.setRequestMethod ("POST");
conn.connect ();

/ / Send data
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new GZIPOutputStream (conn.getOutputStream ()), "GBK"));
bw.write (content);
bw.flush ();
bw.close ();


/ / Receive data back
StringBuffer sb = new StringBuffer ();
BufferedReader br = new BufferedReader (new InputStreamReader (new GZIPInputStream (conn.getInputStream ()), "GBK"));
String str = null;
while ((str = br.readLine ())! = null) {
sb.append (str);
}
br.close ();

conn.disconnect ();
return sb.toString ();
}


Server-side code:
 / ** 
* Receive HTTP data
* @ Param request
* @ Param response
* @ Return data
* /
public static String receive (HttpServletRequest request) {
StringBuffer sb = new StringBuffer ();
BufferedReader br = null;
try {
br = new BufferedReader (new InputStreamReader (new GZIPInputStream (request.getInputStream ()), "GBK"));
String str = null;
while ((str = br.readLine ())! = null) {
sb.append (str);
}
br.close ();

} Catch (Exception e) {
e.printStackTrace ();
} Finally {
try {
if (br! = null) br.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
}
return sb.toString ();
}

/ **
* HTTP response data
* @ Param responseText
* @ Param response
* /
public static void response (String responseText, HttpServletResponse response) {
BufferedWriter bw = null;
try {
response.setHeader ("Content-Encoding", "gzip");
bw = new BufferedWriter (new OutputStreamWriter (new GZIPOutputStream (response.getOutputStream ()), "GBK"));
bw.write (responseText);
bw.flush ();
bw.close ();

} Catch (Exception e) {
e.printStackTrace ();
} Finally {
try {
if (bw! = null) bw.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
}
}


I wrote these two tools, you can try intentioned, Android buttons built on the line, the server build a Servlet on the line.

It's that dotted, thank you! ! !
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
By the way, the exception is: java.io.IOException: Unknown format
In the Android side:
 BufferedReader br = new BufferedReader (new InputStreamReader (new GZIPInputStream (conn.getInputStream ()), "GBK")); 

Thrown out
Reply:
Looks like the JDK GZIP itself is a problem, you read this article useful to you: http:/ / cin-ie.iteye.com/blog/859822
Reply:
Back to the second floor, try it today, do not look into
Reply:
Really is such a thing, so put enough
Reply:
Do not put so that, hey

No comments:

Post a Comment