我是一名java开发人员,就在今天,我开始为这个项目学习C#。我需要创建一个从wsdl调用服务的客户端。我已经把这个部分运行了。我生成了一个C#类,它通过“wsdl.exe”将wsdl包装到服务中。我也能够实现它,我的问题是,我需要在发送请求时使用wsse头。默认情况下wsdl.exe没有提供,所以我稍微调整了我的代码,以便我可以使用wsse头发送请求。这是我的代码片段,用于调用服务:
using Microsoft.Web.Services3.Security.Tokens;
using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Security;
static void Main(string[] args)
{
//... some other code here ...
//create a credentials
UsernameToken usernameToken = new UsernameToken("myUsername", "myPassword");
//create a new instance of ApiService.
ApiService apiClient = new ApiService();
apiClient.Url = "http://something.com/something/soap/v2/";
SoapContext context = apiClient.RequestSoapContext;
context.Security.Tokens.Add(usernameToken);
context.Security.Elements.Add(new MessageSignature(usernameToken));
//call the sendFax method.
myResponse response = apiClient.SendMyRequest(myRequest);
}
这是我在调用“sendRequest()
”时调用的代码片段。
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
[return: System.Xml.Serialization.XmlElementAttribute("MyResponse", Namespace="https://something.com/something/soap/v2", IsNullable=true)]
public myResponse SendMyRequest ([System.Xml.Serialization.XmlElementAttribute(Namespace="https://something.com/something/soap/v2", IsNullable=true)] myRequest MyRequest) {
Console.WriteLine("sending message... ");
object[] results = this.Invoke("SendMyRequest", new object[] {
MyRequest});
return ((myResponse)(results[0]));
}
//some other code here.
每当我点击跑步时,我都会收到错误:
The security token could not be authenticated or authorized
现在,我的问题是:
在这段代码中:
[something]
< ----你怎么称呼它?
public class hello {
//some code here
}
是的,它叫什么?请原谅我,这种语法对我来说有点新鲜。告诉我它的用语,我会在google上搜索。