【问题标题】:SOAP Request failing from Windows Service来自 Windows 服务的 SOAP 请求失败
【发布时间】:2011-03-23 00:34:08
【问题描述】:

我正在使用以下代码创建 SOAP 请求

    public static string SubmitSoapRequest(string url,
                                                        string ns,
                                                        string operation,
                                                        string requestXml,
                                                        bool responseNodeOnly)
    {
        // make sure the namespace has a trailing slash
        ns = ns.TrimEnd('/') + '/';

        // format soap request
        string soap = string.Format(SOAP_REQUEST_XML, ns, operation, requestXml);

        // format soap action
        string soapAction = string.Format(@"{0}{1}", ns, operation);

        // initialize web request
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        // add header and other soap params
        req.Headers.Add("SOAPAction", soapAction);
        req.ContentType = @"text/xml;charset=""utf-8""";
        req.Accept = "text/xml";
        req.Method = "POST";

        // make soap request
        using (Stream s = req.GetRequestStream())
        using (StreamWriter sw = new StreamWriter(s))
            sw.Write(soap);

        // get response from remote web-service
        WebResponse response = req.GetResponse();

从已编译的 Windows 应用程序运行时可以正常工作,但从 Windows 服务调用时会失败。从异常中捕获了“500 内部服务器错误”。

是否有任何原因导致通过 Windows 服务运行可能会导致这种情况?

提前感谢您的任何帮助/建议

【问题讨论】:

    标签: c# soap


    【解决方案1】:

    没有看到服务发生了什么,我最初的猜测是它与凭据相关。远程服务是否需要特定凭据才能访问?当您将代码作为 Windows 应用程序运行时,传递给服务的凭据可能是您的网络凭据;但该服务正在传递它的凭据(网络服务)。

    在发出服务请求之前,您可能希望在请求中设置适当的凭据。这是我下车的一个例子MSDN

    CredentialCache myCredentialCache = new CredentialCache();
    myCredentialCache.Add(new Uri("http://foo.com/"),"Basic", new NetworkCredential("user1","passwd1","domain1"));
    WebRequest myWebRequest = WebRequest.Create("http://foo.com");
    NetworkCredential myCredential = myCredentialCache.GetCredential(new Uri("http://foo.com"),"Basic");
    myWebRequest.Credentials = myCredential; 
    WebResponse myWebResponse = myWebRequest.GetResponse();
    

    在没有看到服务的情况下,这将是根据您描述的情况进行初步调查的事情。我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 1970-01-01
      • 2016-05-20
      • 2010-11-22
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多