我希望有人可以提供实际的例子
我从各种帖子中得到了这么多,但我一直得到不好的请求。
基本上我想要实现的是使用AppToken键登录,然后从Response中检索SessionToken。这是他们从API文档中发布的信息
响应如下(使用简单REST客户端时)
<Login xmlns="http://schemas.datacontract.org/2004/07/Tamigo.Services.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DefaultCompanyId i:nil="true"/><DefaultCompanyName i:nil="true"/><DefaultDepartmentId i:nil="true"/><DefaultDepartmentName/><Email/><EmployeeId i:nil="true"/><ImageUrl i:nil="true"/><MenuId>0</MenuId><Name i:nil="true"/><Password/><Role>Application</Role><SessionToken>e1f35353-08f7-4213-a6b9-251313b36701</SessionToken></Login>
从那个响应中我需要以某种方式获得&#34; SessionToken&#34;到下一个GET请求中使用的变量。
这是我到目前为止编写的代码,这是我可以从其他类似问题中找到的代码片段(当然,这些代码并不适合我显然完全独特/从未见过的类型请求) :
using System;
using System.Xml;
using System.Net;
using System.Text;
using System.IO;
namespace RESTServicesXMLParseExample
{
class Program
{
static void Main(string[] args)
{
try
{
var request =
(HttpWebRequest)WebRequest.Create("https://api.tamigo.com/login/application");
var postData = "Name=test";
postData += "&Key=y3LIZ5u5yR9A7a98ypBdygQyIBrqQwZdfdfZKmgtErQ=";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
//Create the REST Services 'Find by Query' request
XmlDocument locationsResponse = MakeRequest(responseString);
ProcessResponse(locationsResponse);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
}
}
//Submit the HTTP Request and return the XML response
public static XmlDocument MakeRequest(string requestUrl)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());
return (xmlDoc);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
return null;
}
}
static public void ProcessResponse(XmlDocument locationsResponse)
{
//Create namespace manager
XmlNamespaceManager nsmgr = new XmlNamespaceManager(locationsResponse.NameTable);
nsmgr.AddNamespace("rest", "http://schemas.datacontract.org/2004/07/Tamigo.Services.Entities");
XmlNodeList formattedAddressElements = locationsResponse.SelectNodes("//rest:SessionToken", nsmgr);
Console.WriteLine("SessionToken");
foreach (XmlNode SessionToken in formattedAddressElements)
{
Console.WriteLine(SessionToken.InnerText);
}
Console.WriteLine();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}
答案 0 :(得分:0)
如果您的API文档告诉您发送应用程序/ json有效负载,那么您最好不要忽略它,因为内容类型application/x-www-form-urlencoded
实际上是不同的。
通过以下更改,我获得了401 Unauthorized,因为我希望密钥和/或名称无效。所以我解决了Bad Request的问题。
var request = (HttpWebRequest)WebRequest.Create("https://api.tamigo.com/login/application");
request.Method = "POST";
request.ContentType = "application/json";
// create the serializer for the Login class
var ser = new DataContractJsonSerializer(typeof(Login));
// set our login values
var login = new Login{ Name="test", Key ="y3LIZ5u5yR9A7a98ypBdygQyIBrqQwZdfdfZKmgtErQ=" };
// serialize the Login instance directly to the request stream
ser.WriteObject(request.GetRequestStream(), login);
// handle response
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
这是我使用的小助手类,因此DataContractJsonSerializer可以完成它的工作:
public class Login
{
public string Name;
public string Key;
}