从API发送的XML
<;身份验证结果xmlns:i=”http://www.w3.org/2001/XMLSchema-instance“>;
<;错误代码>;0</错误代码>;
<;ErrorMessage/>;
<;AccessToken>;StringAccessToken</AccessToken>;
<;AccessSecret>;StringAccessToken</AccessSecret>;
<;PolarisUserID>;PolarisSampleUser</PolarisUserID>;
<;布兰奇德>;7</布兰奇德>;
<;AuthExpDate>;2013-05-27T16:57:46.323</AuthExpDate>;
</认证结果>;
响应类
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Xml;
使用System.Xml.Schema;
使用System.Xml.Serialization;
命名空间自动测试工具
{
[XmlRoot(ElementName=“AuthorizationResult”)]
公共类授权结果
{
公共错误代码{get;set;}
公共字符串错误消息{get;set;}
公共字符串AccessToken{get;set;}
公共字符串AccessSecret{get;set;}
公共int PolarisUserID{get;set;}
公共整数{get;set;}
公共日期时间AuthExpDate{get;set;}
}
}
代码生成请求和反序列化
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
Net系统;
使用System.Web;
使用System.Web.Script.Serialization;
使用System.Security.Cryptography;
使用System.Xml;
使用System.Xml.Serialization;
命名空间自动测试工具
{
公共类方法
{
公共字符串URI{get;set;}
公共字符串accSecret{get;set;}
公共字符串accToken{get;set;}
公共字符串授权字符串{get;set;}
公共bool AuthenticateStaffUser()
{
尝试
{
//初始化所有变量
字符串authReqMethod=“POST”;
字符串authAccessKey=“示例访问密钥”;
字符串authAccessKeyID=“示例访问ID”;
字符串authorsupportpassword=“”;
DateTime authDateTime=DateTime.Now;
字符串httpAuthDateTime=authDateTime.ToUniversalTime().ToString(“r”);
字符串authURI=“示例URI”;
//根据给定的参数组合哈希
字符串papiHash=GetPAPIHash(authAccessKey、authReqMethod、authURI、httpAuthDateTime、authPassword);
//格式化授权字符串
string authorizationString=string.Format(“授权:PWS{0}:{1}”,authAccessKeyID,papiHash);
//创建和定义WebRequest
WebRequest req=WebRequest.Create(authURI);
请求方法=“POST”;
请求标题添加(“PolarisDate”,httpAuthDateTime);
请求头添加(授权字符串);
req.ContentType=“应用程序/xml”;
string requestBody=“<;AuthenticationData>;<;Domain>;SampleDomain<;/Domain>;<;Username>;SampleUsername<;/Username>;<;Password>;SamplePassword<;/Password>;<;/AuthenticationData>;”;
byte[]reqBodyBytes=System.Text.Encoding.UTF8.GetBytes(requestBody);
req.ContentLength=reqBodyBytes.Length;
使用(Stream requestStream=req.GetRequestStream())
{
写入(reqBodyBytes,0,reqBodyBytes.Length);
}
//接收WebResponse
使用(WebResponse resp=req.GetResponse())
{
AuthorizationResult firstResponse=新的AuthorizationResult();
Stream respStream=resp.GetResponseStream();
StreamReader sr=新的StreamReader(respStream);
XmlSerializer XmlSerializer=新的XmlSerializer(typeof(AuthorizationResult));
firstResponse=(AuthorizationResult)xmlSerializer.Deserialize(respStream);
Console.WriteLine(“授权:PWS”+firstResponse.AccessSecret+”:“+firstResponse.AccessToken”);
返回true;
}
}
捕获(例外情况除外)
{
控制台写入线(ex);
返回false;
}
}
公共字符串GetPAPIHash(字符串strAccessKey、字符串strHTTPMethod、字符串strURI、字符串strhttpupdate、字符串strPatronPassword)
{
byte[]secretBytes=UTF8Encoding.UTF8.GetBytes(strAccessKey);
HMACSHA1 hmac=新的HMACSHA1(secretBytes);
字节[]数据字节=null;
如果(strPatronPassword.Length>;0)
{
dataBytes=UTF8Encoding.UTF8.GetBytes(strHTTPMethod+strURI+strHTTPDate+strPatronPassword);
}
其他的
{
dataBytes=UTF8Encoding.UTF8.GetBytes(strHTTPMethod+strURI+strHTTPDate);
}
字节[]computedHash=hmac.ComputeHash(数据字节);
string computedHashString=Convert.ToBase64String(computedHash);
返回computedHashString;
}
}
}
我正在向API发出一个包含AuthorizationData的POST请求。API应该返回xml以反序列化为firstresponse。我已收到xml(通过打印到控制台确认),但我正在收到xml文档(1,2)和中存在错误<;授权数据xmlns=”“>不是预期的。提前谢谢你的帮助
它似乎在抱怨一个意外的根元素,并且在您的问题的其他地方围绕着它存在一些混乱
问题标题中有<;授权结果>
在示例XML响应中,您有<;认证结果>
在C#中有[XmlRoot(ElementName=“AuthorizationResult”)]
在问题的末尾,您提到了<;授权数据>
这是一个主题上相当微妙的变化
假设XML示例响应是正确的,您应该更改C#以期望根元素是<;认证结果>
[XmlRoot(ElementName=“AuthenticationResult”)]
公共类身份验证结果
{
...