SOAP请求从Windows服务
问题描述:
失败我用下面的代码SOAP请求从Windows服务
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应用程序运行时,该工作正常创建SOAP请求,但是从Windows服务调用时失败。从异常中捕获'500内部服务器错误'。
是否有任何理由为什么通过Windows服务运行可能导致这种情况?
在此先感谢您的帮助/咨询
答
没有看到发生了什么事情的服务,我最初的猜测是,它的凭据相关。远程服务是否需要特定的访问凭据?当您将代码作为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();
没有看到该服务,这将是一些根据您所描述的情况初步调查。我希望这有帮助!!