-1

我正在点击一个网址作为以下示例:

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 LibWebClient但我把它改成HttpWebRequest假设它会是一个修复。有什么建议么?

4

1 回答 1

0

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

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

于 2013-08-22T20:11:54.813 回答