I would like to fetch a html page with post method. First I use HttpClient, it doesn’t give the right response, then I add Header with a Referer, it works well but too slow. So I plan to use URLConnection, I also add Referer to the Header, this time it doesn’t return what I want.
HttpClient:
httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(WhutGlobal.URL_HEADER_STR + "xscj_gc.aspx?xh=" + WhutGlobal.USER_ID + "&xm=" + WhutGlobal.USER_NAME + "&gnmkdm=N121605");
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader("Referer", WhutGlobal.URL_HEADER_STR + "xscj_gc.aspx?xh=" + WhutGlobal.USER_ID + "&xm=" + WhutGlobal.USER_NAME + "&gnmkdm=N121605");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(is);
byte bytearray[] = new byte[800000];
int current= -1;
int i=0;
while((current=bis.read())!=-1) {
bytearray[i] =(byte) current;
i++;
}
html = new String (bytearray,"GB2312");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
URLConnection:
URL url = new URL(WhutGlobal.URL_HEADER_STR + "xscj_gc.aspx?xh=" + WhutGlobal.USER_ID + "&xm=" + WhutGlobal.USER_NAME + "&gnmkdm=N121605");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Accept-Encoding", "gzip");
urlConnection.setRequestProperty("Referer", WhutGlobal.URL_HEADER_STR + "xscj_gc.aspx?xh=" + WhutGlobal.USER_ID + "&xm=" + WhutGlobal.USER_NAME + "&gnmkdm=N121605");
DataOutputStream wr = new DataOutputStream (
urlConnection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
There should be no significant difference in speed between
HttpClientandURLConnectionmethods of obtainin html … it can depends on connection speed (3G/Wifi)butHttpPost solution can work slow because you’re using:
while((current=bis.read())!=-1) ...instead this STUPID snipet(in this case reading byte by byte is really not efficient) use:
html = EntityUtils.toString(entity, "GB2312")about HttpURLConnection … it really depends on what you send in urlParameters … and you should not use
DataOutputStreamin this case