1

There is a URL which opens fine on PC and mobile browser (chrome), but when i try to open it in an android program using below code it gives following error.

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
  String response = "";
  for (String url : urls) {
    //DefaultHttpClient client = new DefaultHttpClient();
      HttpClient client = AndroidHttpClient.newInstance("Android");
    HttpGet httpGet = new HttpGet(url);
   // httpGet.setHeader("User-Agent", "Chrome");
    try {
      HttpResponse execute = client.execute(httpGet);
      InputStream content = execute.getEntity().getContent();

      BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
      String s = "";
      while ((s = buffer.readLine()) != null) {
        response += s;
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  return response;
}

on running the code I get an Access Denied error from the server. If I try any other url it works fine. I have tried searching on how to set user-agent to bypass any restrictions put on the server, but could not succeed. Please help.

Additionally I have seen that the header section of the webpage from which this url is called (from browser) contains the below

 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
 <meta name="apple-mobile-web-app-capable" content="yes" />
 <meta name="HandheldFriendly" content="true" />
 <meta name="MobileOptimized" content="width" />
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4

1 回答 1

1

我创建了一个php文件如下

<?php
$headers = apache_request_headers();

foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
?>

我从 PC 浏览器和 Android 代码中调用了这个页面。使用 httpGet.setHeader(name,value) 在 Android 代码中添加了来自 PC 浏览器的所有标头值

它现在正在工作。:)

于 2013-06-01T18:50:05.457 回答