无法从传输连接中读取数据:现有连接被C#中的远程主机强行关闭 [英] Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host in C#

查看:602
本文介绍了无法从传输连接中读取数据:现有连接被C#中的远程主机强行关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,用于发送URL请求并接收响应并将其存储为String as

I have a code where I am sending the URL request and receiving the response and storing it as a String as

public String GenerateXML(String q)// Here 'q' is the URL 
{
    // Generating the XML file for reference
    // Getting the response in XML format from the URL

    Debug.WriteLine("The Http URL after URL encoding :" + q);
    try
    {
        Uri signs1 = new Uri(q);
        //Debug.WriteLine("The Requested URL for getting the XML data :" + re);

        WebRequest request1 = WebRequest.Create(signs1);

        HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();

        //HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();

        Stream receiveStream = response1.GetResponseStream();

        StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

        String ab = readStream.ReadToEnd();// The mentioned error is showing up here.
        // Debug.WriteLine("The data :"+a);
        //XmlDocument content2 = new XmlDocument();

        // content2.LoadXml(ab);

        //  content2.Save("C:/Users/Administrator/Downloads/direct.xml");
        return ab;
    }
    catch (System.Net.WebException ex)
    {
        Debug.WriteLine("Exception caught :" + ex);
        return null;
    } 
}

为什么连接被远程主机关闭? 消除错误或至少忽略该错误并继续其他URL请求的可能性是什么?我已经包含了try and catch以便逃避任何错误并继续运行而不会停止.在互联网上搜寻解决方案,但是针对此特定问题的解决方案非常具体.请任何帮助表示赞赏. 预先感谢.

Why is the connection closed by the remote host? What are the possibilities of getting rid of the error or at least ignore the error and continue with other URL requests? I have included try and catch so as to escape any error and continue functioning with out any stop. Scoured the internet for solution but solutions to this particular problem is pretty much specific. Please any help is appreciated. Thanks in advance.

推荐答案

实际的异常可能是IOException-您需要捕获该异常类型以及WebException.实际的问题可能是您的URL已过期,并且系统不再运行Web服务器,或者也许,该请求需要进行身份验证/需要使用@ L.B建议的标头.

The actual exception is probably an IOException - you would need to catch that exception type as well as WebException. The actual problem may be that your URL is out of date and the system is no longer running a web server, or perhaps, the request needs to be authenticated/needs a header as @L.B suggests.

此外,您可能正在泄漏各种资源.您应该将WebResponse和流包装在using语句中.

Also, you are potentially leaking all sorts of resources. You should be wrapping your WebResponse and streams in using statements.

using (var response = (HttpWebResponse)request.GetResponse())
using (var receiveStream = response.GetResponseStream())
using (var reader = new StreamReader(receiveStream))
{
     var content = reader.ReadToEnd();
     // parse your content, etc.
}

这篇关于无法从传输连接中读取数据:现有连接被C#中的远程主机强行关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆