[英]convert curl php rest api calls to c# code
I've been trying to convert php code that accesses a json rest api into c# but cannot get this to work... 我一直在尝试将访问json rest api的php代码转换为c#,但无法正常工作...
I cannot get a valid response back.. (400 Bad request) 我无法获得有效的回复。(400错误的请求)
Here is the curl im trying to convert: 这是我试图转换的curl:
curl --user bob@gmail.com:387653t253431a1b1d6687pl9836th5s \
--form url=http://bbc.com --form x-metrix-adblock=0 \
https://gtmetrix.com/api/0.1/test
Here is the code I've tried without success: 这是我尝试失败的代码:
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();
}
any help would be appreciated.. 任何帮助,将不胜感激..
This is not a direct answer to your question, but I wanted to let you know that I've released an open source .Net client library which simplifies consuming the GTMetrix.net API. 这不是您问题的直接答案,但我想告诉您,我已经发布了一个开源.Net客户端库,该库简化了GTMetrix.net API的使用。 Submitting a test and retrieving the results is simple;
提交测试并检索结果很简单;
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;
...
}
The project (docs and code) can be found here GTmetrix-net on GitHub 可以在GitHub GTmetrix-net上找到该项目(文档和代码)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.