我得到了一个配置文件,其中包含端点地址,用户名和密码。有人告诉我写一种称呼该肥皂端点的方法,但是经过几个小时的研究,我仍然无法将这些碎片放在一起。请参阅下面的配置文件。
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Username" value="username"/>
<add key="Password" value="password"/>
</appSettings>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="SecureBinding">
<textMessageEncoding messageVersion="Soap11"/>
<security authenticationMode="UserNameOverTransport" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" enableUnsecuredResponse="true"/>
<httpsTransport/>
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://xxxxxxxxxxxx/ExecutePortType" binding="customBinding" bindingConfiguration="SecureBinding" contract="com.company.ExecutePortType" name="ExecutePortType"/>
</client>
<extensions>
<bindingElementExtensions>
<add name="customTextMessageEncoding" type="WsSecurityMessageEncoder.CustomTextMessageEncodingElement, WsSecurityMessageEncoder"/>
</bindingElementExtensions>
</extensions>
</system.serviceModel>
<system.web>
<compilation debug="true" targetFramework="4.6"/>
</system.web>
</configuration>
我不断获得
远程服务器返回了一个错误:(401(未经授权。
,这就是我到目前为止的代码中所拥有的。我复制并从中粘贴了很多此链接
private readonly Uri _Address;
private readonly string _Username;
private readonly string _Password;
public KeyReaderRepository()
{
_Username = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["naServiceUserName"]);
_Password = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["naServicePassword"]);
var KeyreaderUrl = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["KeyreaderEndpoint"]);
_Address = new Uri(KeyreaderUrl);
}
public string DecryptKeyReader()
{
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest();
//InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
Stream stream = webRequest.GetRequestStream();
soapEnvelopeXml.Save(stream);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
//// suspend this thread until call is complete. You might want to
//// do something usefull here like update your UI.
//asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
return "";
}
private HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_Address);
webRequest.Headers.Add("SOAPAction", "");
webRequest.ContentType = "application/soap+xml;charset=UTF-8";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.Credentials = new NetworkCredential(_Username, _Password);
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><HelloWorld xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>");
return soapEnvelopeDocument;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
有人可以告诉我我做错了什么吗?还是我缺少什么?谢谢
您是否检查过_Username
和_Password
变量是否正确设置?
因为您在配置文件中设置了用户名和密码:
<appSettings>
<add key="Username" value="username"/>
<add key="Username" value="password"/>
</appSettings>
我认为您应该将文件更改为:
<appSettings>
<add key="Username" value="username"/>
<add key="Password" value="password"/> <------ Password instead of Username!?
</appSettings>