【问题标题】:HttpwebRequest Crashes without exception when hiting a mobile urlHttpwebRequest 在点击移动 url 时无异常崩溃
【发布时间】:2013-08-22 18:51:40
【问题描述】:

我正在点击如下示例的网址:

http://mobile.example.com/ip/someProduct-fl-oz/productID

我的要求是:

    public static string getMobileHtml(string url)
    {
        string responseData = "";
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            request.Timeout = 10000;
            request.Host = "mobile.example.com";
            request.Method = "GET";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(responseStream);
                responseData = myStreamReader.ReadToEnd();
            }
            response.Close();
        }
        catch (Exception e)
        {
            responseData = "An error occurred: " + e.Message;
        }
        return responseData;
    }

而且代码在线崩溃:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

没有任何例外,只是中断了,Curl Lib,WebClient 也发生了同样的事情,但我将其更改为 HttpWebRequest,假设这将是一个修复。有什么建议吗?

【问题讨论】:

  • 您的网址是否以“http”开头?我还建议您在这里阅读“request.host”属性msdn.microsoft.com/en-us/library/…
  • @MauricioGracia 是的,在 Ie,FireFox 中输入了相同的 url,我使用 Fiddler 复制了上面的请求,所以这一切都使它工作。它甚至没有到达主机部分,因为我说它在顶部坠毁。
  • 如果你使用“WebRequest webRequest = WebRequest.Create(url);”还是失败了?
  • @MauricioGracia 让我试试...
  • @MauricioGracia 嘿,它适用于 WebResponse 和 WebRequest 但我仍然无法在返回任何内容之前获得它关闭的 html,这是为什么?

标签: c# httpwebrequest web-scraping


【解决方案1】:

我测试了您发布的代码,并且成功创建了 HttpWebRequest,然后它在响应行中失败了,但在我的情况下是因为我在防火墙后面并且我没有提供任何凭据。

问题被诊断为 Fiddler 影响了您的测试,因此关闭它可以解决问题

【讨论】:

    猜你喜欢
    • 2022-10-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2019-05-03
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多