开发者

HttpWebRequest.GetResponse failing continuously until program reset

开发者 https://www.devze.com 2023-04-08 21:08 出处:网络
Occasionally on some machines, a web request will fail with the following message and continue to do so until the program is reset at which time it will start working again. Has anyone else experience

Occasionally on some machines, a web request will fail with the following message and continue to do so until the program is reset at which time it will start working again. Has anyone else experienced this and is there any programmatic way to prevent it?

Message: The remote name could not be resolved: 'www.validwebsite.com' Source: System StackTrace: at System.Net.HttpWebRequest.GetResponse()

EDIT: This is the code the that is being executed

    virtual public string Execute(string web_uri,string request_method,string data = null, string content_type = null)
    {
        WebRequest webRequest = WebRequest.Create(web_uri);
        webRequest.Method = request_method;
        if (content_type != null)
        {
            webRequest.ContentType = content_type;
        }
        webRequest.Method = request_method;
        byte[] bytes = new byte[0];
        if (!string.IsNullOrEmpty(data))
        {
            bytes = Encoding.ASCII.GetBytes(data);
            // send the Post
            webRequest.ContentLength = bytes.Length;   //Count bytes to send
        }
        else
        {
            webRequest.ContentLength = 0;
        }
        if (webRequest.Method == "POST")
        {
            using (Stream os = webRequest.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length);         //Send it
            }
        }
        using (HttpWebResponse webResponse =(HttpWebResponse)webRequest.GetResponse())
        {
            if (webResponse != null)
            {
                ResponseCode = webResponse.StatusCode;
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(),Encoding.UTF8))
                {
                    string xml_string = sr.ReadToEnd().Trim();
                    return xml_string;
             开发者_如何学C   }
            }
        }
        return null;


Put an entry in the hosts file to rule out a DNS issue. If your app then works consistently, you found the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消