0

我已经尝试获取此 webapge http://www.mindef.gov.sg/content/imindef/press_room/official_releases/nr/2013/jan/22jan13_nr.html的 html 源代码。但是,与我从浏览器中看到的内容相比,我遇到了错误,因为它以不同类型的 html 响应。似乎在网上和应用程序上做一个 httopost 会导致不同类型的响应

address="http://www.mindef.gov.sg/content/imindef/press_room/official_releases/nr/2013/jan/22jan13_nr.html";
        String result = "";
        HttpClient httpclient = new DefaultHttpClient();

    //  httpclient.getParams().setParameter("http.protocol.single-cookie-header", true);
        HttpProtocolParams.setUserAgent(httpclient.getParams(),  "Mozilla/5.0 (Linux; U; Android 2.2.1; en-ca; LG-P505R Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");           
InputStream is = null;
HttpGet httpGet = new HttpGet (address);

                HttpResponse response = httpclient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            InputStream is = null;
4

3 回答 3

0

尝试这个:

StringBuilder builder = new StringBuilder();
String line = null;
HttpGet get = new HttpGet("http://www.url.com");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null) builder.append(line);

然后,页面源应该在builder.

于 2013-01-24T18:16:27.173 回答
0

尝试 :

    URLConnection  cn= new URL(url).openConnection();

    BufferedReader input = new BufferedReader( new InputStreamReader( cn.getInputStream() ) );

读取输入流。

于 2013-01-24T18:10:53.830 回答
0

听起来您正在获得该网站的移动版本。如果您扩展 URL 以包含?siteversion=pc您应该将页面提供给您计算机上的浏览器。

于 2013-01-24T18:11:29.180 回答