WebRequest的相当于curl命令 [英] WebRequest Equivalent to CURL command
本文介绍了WebRequest的相当于curl命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我敲我的头撞墙试图到工作curl命令转换为C#的WebRequest。
I am banging my head against a wall trying to convert a working curl command to a c# WebRequest.
我经历了不少贴子阅读,我的是的肯定我有正确的代码,但它仍然是行不通的。
I have read through quite a few postings and I was pretty sure I had the code right but it still will not work.
任何人都可以看到我做错了吗?
Can anyone see what I am doing wrong please?
下面是工作curl命令:
Here is the working curl command:
curl -k -u x:reallylongstring -H "Content-Type: application/json" https://api.somewhere.com/desk/external_api/v1/customers.json
这是我写在C#代码:
WebRequest wrGETURL;
wrGETURL = WebRequest.Create("https://api.somewhere.com/desk/external_api/v1/customers.json");
wrGETURL.Method = "GET";
wrGETURL.ContentType = "application/json";
wrGETURL.Credentials = new NetworkCredential("x", "reallylongstring");
Stream objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string responseFromServer = objReader.ReadToEnd();
但是API响应:
But the api responds:
The remote server returned an error: (406) Not Acceptable.
任何帮助将非常感谢!
Any help would be much appreciated!
感谢
推荐答案
根据尼可拉斯指针我似乎用下面的代码已经解决了这个问题:
Based on Nikolaos's pointers I appear to have fixed this with the following code:
public static gta_allCustomersResponse gta_AllCustomers()
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.somewhere.com/desk/external_api/v1/customers.json");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "*/*";
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("Authorization", "Basic reallylongstring");
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
gta_allCustomersResponse answer = JsonConvert.DeserializeObject<gta_allCustomersResponse>(streamReader.ReadToEnd());
return answer;
}
}
这篇关于WebRequest的相当于curl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文