C#WebRequest不适用于特定链接

时间:2016-07-12 19:52:15

标签: c# .net iis httpwebrequest httpwebresponse

我正在尝试使用下面的代码ping一个链接,但它失败了。它在我们的开发环境中工作正常,但不适用于生产环境。但是,这是一个有效的URL,因为我可以将其复制并粘贴到浏览器中并转到该站点。由于这是在生产中,我不能在我的机器上本地调试它。谁能想到可能导致这种情况的原因?问题只出在这个环节;其他链接工作正常。我正在尝试的链接是https://owl.english.purdue.edu/

System.Net.WebResponse objWebResponse = default(System.Net.WebResponse);
        System.Net.WebRequest objWebRequest = System.Net.WebRequest.Create(URL);
        objWebRequest.Timeout = TimeoutInMiliseconds;
try
        {
            PerformanceTimer.Start();
            objWebResponse = objWebRequest.GetResponse();
            PerformanceTimer.Finish();

            try
            {
                this._ResponseTime = PerformanceTimer.ElapsedTime.Miliseconds;
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
                this._ResponseTime = -1;
            }


            {
                this._ContentLength = objWebResponse.ContentLength;
                this._ContentType = objWebResponse.ContentType;

                int BytesRead = 0;
                byte[] buffer = new byte[10001];
                if (GetFileContent)
                {
                    System.IO.Stream ReceiveStream = objWebResponse.GetResponseStream();
                    System.IO.MemoryStream MS = new System.IO.MemoryStream();
                    //memory stream is used so we can grab binary data 
                    do
                    {
                        BytesRead = ReceiveStream.Read(buffer, 0, buffer.Length);
                        if (BytesRead <= 0) break;

                        MS.Write(buffer, 0, BytesRead);
                    }
                    while (true);
                    this._MemoryStream = MS;
                    ReceiveStream = null;
                }
                else
                {
                    //This else is to validate the existence of the file, but not get their contents in case the file is big. 
                    BytesRead = objWebResponse.GetResponseStream().Read(buffer, 0, buffer.Length);
                    if (BytesRead > 0)
                    {
                        this._MemoryStream = new System.IO.MemoryStream();
                        this._MemoryStream.Write(buffer, 0, BytesRead);
                    }
                }
                objWebResponse.Close();
            }

            functionReturnValue = true;
        }
        catch (System.Net.WebException ex)
        {

            this._Message = ex.Message;
        }
        catch (Exception ex2)
        {

            this._Message = ex2.Message;
        }


        objWebResponse = null;
        objWebRequest = null;
        PerformanceTimer = null;
        return functionReturnValue;

0 个答案:

没有答案