我使用以下代码连接huawei hilink,但它总是返回错误。
首先我通过
获取令牌和会话http://192.168.8.1/api/webserver/SesTokInfo
你能帮我解决一下这个问题吗?
string resulta = PostAndGetHTML("http://192.168.8.1/api/webserver/SesTokInfo");
string tok = resulta.Substring(57, 32);
string ses = resulta.Substring(108, 128);
Postxml("http://192.168.8.1/api/user/login", tok, ses);
public string PostAndGetHTML(string targetURL)
{
string html = string.Empty;
string url = targetURL;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
return html;
}
public void Postxml(string destinationUrl, string token, string ses)
{
var client = new RestClient();
string rawXml = @"<?xml version=""1.0"" encoding=""utf-8""?><request><Username>admin</Username><Password>" + Base64Encode("admin") + @"</Password></request>";
client.BaseUrl = new Uri(destinationUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("__RequestVerificationToken", token);
request.AddCookie("cookie",ses);
request.Timeout = 3000;
request.ReadWriteTimeout = 3000;
request.AddParameter("text/html", rawXml, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
}