我一直试图将访问json rest api的php代码转换为c#,但无法使其工作......
我无法得到有效回复..(400 Bad request)
以下是我试图转换的卷曲:
curl --user bob@gmail.com:387653t253431a1b1d6687pl9836th5s \
--form url=http://bbc.com --form x-metrix-adblock=0 \
https://gtmetrix.com/api/0.1/test
以下是我尝试过的代码没有成功:
content = "http://bbc.com";
string result;
var req = HttpWebRequest.Create("https://gtmetrix.com/api/0.1/test");
req.Method = "POST";
var cc = new CredentialCache();
req.Credentials = new NetworkCredential(username, passkey);
req.ContentType = "application/json";
byte[] bytes = UTF8Encoding.UTF8.GetBytes(content);
req.ContentLength = bytes.Length;
using (var stream = req.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
using (var resp = req.GetResponse())
{
var results = new StreamReader(resp.GetResponseStream()).ReadToEnd();
result = JObject.Parse(results).ToString();
}
任何帮助将不胜感激..
答案 0 :(得分:0)
这不是您问题的直接答案,但我想告诉您我已经发布了一个开源.Net客户端库,它简化了GTMetrix.net API的使用。提交测试并检索结果很简单;
var client = Client(connection);
var request = new TestRequest(
new Uri("http://website.net"),
Locations.London,
Browsers.Chrome);
var response = client.SubmitTestAsync(request);
if (response.Result.StatusCode == HttpStatusCode.OK &&
response.Result.Body.State == ResultStates.Completed)
{
var pageLoadTime = response.Result.Body.Results.PageLoadTime;
...
}
项目(文档和代码)可以在GTmetrix-net on GitHub
找到