使用C#HttpWebRequest从Google Play获取数据并且它的响应与浏览器

时间:2016-09-12 07:49:08

标签: c# google-play httpwebrequest

我尝试使用C#HttpWebRequest从谷歌播放网页获取数据但是当它的响应时我得到了不同的结果

代码:

public const string googlePlayUrl = "https://play.google.com/store/apps/details?id=";

public void GetData(string packageName) {
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(googlePlayUrl + packageName));
    request.Method = WebRequestMethods.Http.Get;
    request.ContentType = "text/html";
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
    request.AutomaticDecompression = DecompressionMethods.GZip;

    request.BeginGetResponse((IAsyncResult asynchronousResult) =>
    {
        HttpWebRequest requested = (HttpWebRequest)asynchronousResult.AsyncState;
        using (HttpWebResponse response = (HttpWebResponse)requested.EndGetResponse(asynchronousResult))
        {
            System.IO.Stream responseStream = response.GetResponseStream();
            using (StreamReader reader = new StreamReader(responseStream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
            responseStream.Close();
        }
    }, request);
}

请求连接很好,我得到了回复,但它与我使用浏览器访问网页时的区别。这不是我想要使用的元素,如

  • div.id-APP-标题
  • span attr [itemprop =“genre”]
  • div attr [itemprop =“description”]

不知道为什么,我尝试设置其用户代理但它仍然不起作用或者我设置错了。 希望有人有解决方案:)

1 个答案:

答案 0 :(得分:0)

假设您的公共IP地址未被Google阻止,您可以将同步方法$state.go('myState', {myParam: {some: 'thing'}}) $stateProvider.state('myState', { url: '/myState', params: {myParam: null}, ... request.GetResponse()一起使用,如下所示:

Parallel.ForEach()

使用public static string GetDataSync(string packageName) { string result = ""; Uri uri = new Uri(googlePlayUrl + packageName); var request = HttpWebRequest.Create(uri); var response = request.GetResponse(); var responseStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(responseStream)) { result = (reader.ReadToEnd()); } responseStream.Close(); return result; } 和踏踏安全的集合Parallel.ForEach调用上面的方法来存储每个包的html字符串结果:

ConcurrentDictionary