发送自定义标头与ksoap2到asp.net

huangapple go评论84阅读模式
英文:

Sending custom header with ksoap2 to asp.net

问题

所以我正在尝试向我的 SOAP 请求头部添加一些身份验证变量,以便对发送方进行身份验证。客户端是 Android 应用程序,服务器使用 ASP.NET Web Forms。

我在客户端使用的库是 ksoap2。我遇到的问题是,当我在服务器端接收到我的 SOAP 封包并检查请求的头部时,我的自定义头部元素不在那里。

唯一存在的键是
"Connection"
"Content-Length"
"Content-Type"
"Accept-Encoding"
"Host"
"User-Agent"
"SOAPAction"
其中自定义的元素名称是 "AuthHeader"(当前的代码来自于另一个线程的回答)。

Java 代码如下所示:

Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element username = new Element().createElement(NAMESPACE, "user");
username.addChild(Node.TEXT, "USERNAME");
h.addChild(Node.ELEMENT, username);
Element pass = new Element().createElement(NAMESPACE, "pass");
pass.addChild(Node.TEXT, "PASSWORD");
h.addChild(Node.ELEMENT, pass);

envelope.headerOut = new Element[1];
envelope.headerOut[0] = h;

Web 方法仅为以下内容:

[WebMethod]
public string ValidateRequest()
{
  Console.WriteLine(HttpContext.Current.Request.Headers.AllKeys);
  return "1.0";
}

有谁可以告诉我我做错了什么吗?

英文:

So i'm trying to add some authentication variables to the header of my soap request. To authenticate the sender. The client is an android app and the server is using asp.net web forms.

The library i'm using on the client side is ksoap2. The problem i am having, is that when i resive my soap envelop on the server side, and check the header of the request, my custom header element is not there.

The only keys which are there are
"Connection"
"Content-Length"
"Content-Type"
"Accept-Encoding"
"Host"
"User-Agent"
"SOAPAction"
Where the name of the custom one is "AuthHeader" (The current code is from an answer, i took from the answer of another thread)

The Java code is written like this

Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element username = new Element().createElement(NAMESPACE, "user");
username.addChild(Node.TEXT, "USERNAME");
h.addChild(Node.ELEMENT, username);
Element pass = new Element().createElement(NAMESPACE, "pass");
pass.addChild(Node.TEXT, "PASSWORD");
h.addChild(Node.ELEMENT, pass);

envelope.headerOut = new Element[1];
envelope.headerOut[0] = h;

The webmethed is just this

[WebMethod]
public string ValidateRequest()
{
  Console.WriteLine(HttpContext.Current.Request.Headers.AllKeys);
  return "1.0";
}

Can any one tell me what i am doing wrong ?

答案1

得分: 0

解决了。
问题是在 asp.net 的 webmethod 中,响应的标头不是 soap envelop 的标头。

要访问 soap envelop 的标头,你需要在 webmethod 类中添加以下内容

public SoapUnknownHeader[] unknownHeaders;
[WebMethod]
[SoapHeader("unknownHeaders", Required = false)]
public string ValidateRequest()
{
  string value = unknownHeaders[0].Element["n0:user"].InnerText;
  Console.WriteLine(value);
  
  return "1.0";
}
/* 输出: "USERNAME" */

位于 WebMethod 标签下的 SoeapHeader 标签会自动将 soap 标头添加到标头数组 "unknownHeaders" 中。然后可以在你的代码中引用它。

至于为什么需要在名称前面添加 n0:,它不知道。

英文:

Solved it.
The problem is that the response's header in asp.net's webmethod, is not the header of the soap envelop.

To gain access to the soap envelop's header, what you need to do it to add this to the webmethod class

public SoapUnknownHeader[] unknownHeaders;
[WebMethod]
[SoapHeader("unknownHeaders", Required = false)]
public string ValidateRequest()
{
  string value = unknownHeaders[0].Element["n0:user"].InnerText;
  Console.WriteLine(value);
  
  return "1.0";
}
/* Output: "USERNAME" */

The SoeapHeader tag under the WebMethod tag, will automatikly add the soap header to the array of headers "unknownHeaders". Which then can be referenced in your code.

And why you need to add the n0: in front of the name, it do not know

huangapple
  • 本文由 发表于 2020年9月30日 23:06:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64140487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定