using System;
using System.Net;
using System.IO;
namespace MakeAGETRequest_charp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
static void Main(string[] args)
{
string sURL;
sURL = "https://secure.logmein.com/public-api/v1/inventory/hardware/reports";
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
wrGETURL.UseDefaultCredentials = true;
wrGETURL.PreAuthenticate = true;
wrGETURL.Credentials = CredentialCache.DefaultCredentials;
wrGETURL.ContentType = "application/json";
//wrGETURL.Headers.Add("Company Id:", "xxxxxxxxx");
// wrGETURL.Headers.Add("PSK:", "0x_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
wrGETURL.Headers[HttpRequestHeader.Authorization] = "companyId":"114xxxxxx", "psk":" 0x_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
WebProxy myProxy = new WebProxy("myproxy", 80);
myProxy.BypassProxyOnLocal = true;
// wrGETURL.Proxy = WebProxy.GetDefaultProxy(); // obsolete not needed
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string sLine = "";
int i = 0;
while (sLine != null)
{
i++;
sLine = objReader.ReadLine();
if (sLine != null)
Console.WriteLine("{0}:{1}", i, sLine);
}
Console.ReadLine();
}
}
}
我正在尝试从Logmein网站上提取报告并根据他们的文档,我真的需要先帮助解决此问题才能获得硬件清单....错误是400个错误请求和长字符
如何通过psk和公司ID对其进行身份验证?
现在使用RESTAPI工作代码并使用我的AUTOit SCRIPT获得类似的结果是空令牌并使令牌过期。 LOgmein服务器的问题,没有给出报告:`using System;
使用System.Net;使用System.IO;使用System.Net.Http;运用 RestSharp;
命名空间MakeAGETRequest_charp {/// /// Class1的摘要描述。 /// 公共类ConsoleApplication3 {
static void Main(string[] args) { var ade = new RestClient("https://secure.logmein.com/"); var request = new RestRequest("public-api/v1/inventory/system/reports", Method.GET); request.AddHeader("authorization", "{\"companyId\":113,\"psk\":\"00_loqxkwbz8w0xxxxxxx\"}"); request.AddHeader("accept", "application/Json; charset=utf-8"); IRestResponse response = ade.Execute(request); //string sURL; var result = ""; result = response.Content; Console.Write(result); Console.Read() //Console.WriteLine(ade.Execute(request)); // Console.Read(); } } } `
答案 0 :(得分:1)
由于我没有实际登录API的参数,因此我无法确定这是否可行。但它确实实现了您referenced
文档中指定的所有要求我使用位于System.Net.Http
程序集内的HttpClient类来执行此示例。
using(var client = new HttpClient())
{
//Base Url
client.BaseAddress = new Uri("http://secure.logmein.com/public-api/v1/");
//Add mandatory Request Headers General parameters Section of doc
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
client.DefaultRequestHeaders.Add("Accept", "application/json; charset=utf-8");
//Authentication header.
string auth = "{\"companyId\":123456,\"psk\":\"ABCZSWDED\"}";
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization:", auth);
HttpResponseMessage response = client.GetAsync("inventory/hardware/reports").Result;
}
唯一的问题是API需要一个带有JSON值的Authorization标头。这不是&#34;默认&#34;授权方法。因此,您需要使用TryAddWithoutValidation
来跳过验证并在请求标头中添加json。
如果这仍然不起作用,您可能需要使用另一个http客户端来发出请求。我已经看到有人使用RestSharp成功登录此API。请参阅此类似question