尝试GetRequestStream()时无法连接到远程服务器

时间:2012-03-19 16:57:30

标签: c# wcf windows-services webrequest

我有一个非常奇怪的问题。

我有由Windows服务托管的wcf调用的方法,而该方法尝试执行GetRequestStream(),我们得到一个异常“无法连接到远程服务器

string strXMLServer = "https://xxxx.webex.com/WBXService/XMLService";

        WebRequest request = WebRequest.Create(strXMLServer);
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        string strXML = XMLmanager.createXML(User, Password, requestType, sessionKey);

        byte[] byteArray = Encoding.UTF8.GetBytes(strXML);

        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;

        // Get the request stream.
        Stream dataStream = request.GetRequestStream();    //<--- here the exception!!
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();

奇怪的是,当我尝试将此应用程序作为stanalone(控制台应用程序)运行时,我没有问题并且没有错误!只有当我通过WCF调用方法时才会出现异常!

1 个答案:

答案 0 :(得分:2)

听起来你和SO question的情况类似。检查运行Windows服务的帐户,它很可能无法访问网络资源。