WebService异常 - 基础连接已关闭:发送时发生意外错误

时间:2014-07-15 20:05:41

标签: c# .net web-services wcf iis

我编写了一个以下方法来从客户端Web服务检索XML响应。 在下面的代码中,我收到错误 {"无法从传输连接读取数据:现有连接被远程主机强行关闭。"}

请帮忙。

以下代码错误

WebResponse webResponse = req.GetResponse();  

调用WebService的方法 -

[WebMethod]
public XmlDocument GetData()
{
    string sError = "";

    string sApiSession;

    DateTime dtRequested = DateTime.Now;

    XmlDocument xmlDoc = new XmlDocument();
    XDocument xDoc = new XDocument();

    try
    {
        NetworkCredential creds = new NetworkCredential();

        creds.UserName = System.Configuration.ConfigurationManager.AppSettings.Get("UserName");
        creds.Password = System.Configuration.ConfigurationManager.AppSettings.Get("Password");
        WebRequest req = WebRequest.Create(System.Configuration.ConfigurationManager.AppSettings.Get("Services"));

        req.Method = System.Configuration.ConfigurationManager.AppSettings.Get("Method");
        req.ContentType = System.Configuration.ConfigurationManager.AppSettings.Get("ContentType ");
        req.Credentials = creds;

        //Retriev into webresponse
        WebResponse webResponse = req.GetResponse();

        //Read from WebResponse to datastream
        Stream dataStream = webResponse.GetResponseStream();

        //read the content of data stream
        using (StreamReader reader = new StreamReader(dataStream))
        {
            xDoc = XDocument.Load(reader);
        }
    }
    catch (Exception ex)
    {
        throw;
    }
    return xmlDoc = getXml(xDoc);
}    

我已经吞下了以下链接,但仍然没有结果。

http://briancaos.wordpress.com/2012/06/15/an-existing-connection-was-forcibly-closed-by-the-remote-host/

1 个答案:

答案 0 :(得分:0)

上面的代码工作正常,如果我尝试从我的办公室网络和盒子。目前我们正在开展网络拦截工作。