2011-07-26 168 views
4

这里是我的HttpClient要求:如何从HttpClient HttpResponse获取源代码?

HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.***.**/***/***_***.php"); 
     String HTML = ""; 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("from", contactName)); 
      nameValuePairs.add(new BasicNameValuePair("msg", message)); 
      nameValuePairs.add(new BasicNameValuePair("sent", time_sent)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      HttpResponse response = httpclient.execute(httppost); 
      HTML = "How?"; 

     } catch (ClientProtocolException e) {} catch (IOException e) {} 

我怎样才能填补HTML字符串与请求的源代码?

回答

5
HttpResponse response = httpclient.execute(httppost); 
HTML = EntityUtils.toString(response.getEntity()); 
4

你去那里:

String html = org.apache.http.util.EntityUtils.toString(response.getEntity()); 
1

您可以通过从httpresponse.getEntity().getContent();

不知怎的,我想是因为你用的是“源代码”,这可能不是回答你的问题读取流得到的回应HTML 。