[英]WebRequest Equivalent to CURL command
I am banging my head against a wall trying to convert a working curl command to ac# WebRequest.我正用头撞墙,试图将工作 curl 命令转换为 ac# 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?谁能看到我做错了什么?
Here is the working curl command:这是工作 curl 命令:
curl -k -u x:reallylongstring -H "Content-Type: application/json" https://api.somewhere.com/desk/external_api/v1/customers.json
And this is the code I have written in c#:这是我用 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();
But the api responds:但 api 响应:
The remote server returned an error: (406) Not Acceptable.
Any help would be much appreciated!任何帮助将非常感激!
Thanks谢谢
Based on Nikolaos's pointers I appear to have fixed this with the following code:根据 Nikolaos 的指示,我似乎用以下代码解决了这个问题:
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;
}
}
Here is my solution to post json data to using an API call or webservice这是我使用 API 调用或网络服务发布 json 数据的解决方案
public static void PostJsonDataToApi(string jsonData)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.somewhere.com/v2/cases");
httpWebRequest.ReadWriteTimeout = 100000; //this can cause issues which is why we are manually setting this
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "*/*";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", "Basic ThisShouldbeBase64String"); // "Basic 4dfsdfsfs4sf5ssfsdfs=="
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
// we want to remove new line characters otherwise it will return an error
jsonData= thePostBody.Replace("\n", "");
jsonData= thePostBody.Replace("\r", "");
streamWriter.Write(jsonData);
streamWriter.Flush();
streamWriter.Close();
}
try
{
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
string respStr = new StreamReader(resp.GetResponseStream()).ReadToEnd();
Console.WriteLine("Response : " + respStr); // if you want see the output
}
catch(Exception ex)
{
//process exception here
}
}
This is the curl command I use to post json data:这是我用来发布 json 数据的 curl 命令:
curl http://IP:PORT/my/path/to/endpoint -H 'Content-type:application/json' -d '[{...json data...}]'
This is equivalent to the above curl command with C#:这等价于使用 C# 的上述 curl 命令:
var url = "http://IP:PORT/my/path/to/endpoint";
var jsonData = "[{...json data...}]";
using (var client = new WebClient())
{
client.Headers.Add("content-type", "application/json");
var response = client.UploadString(url, jsonData);
}
According to this question regarding 406: What is "406-Not Acceptable Response" in HTTP?根据这个关于 406 的问题: HTTP 中的“406-Not Acceptable Response”是什么? perhaps you could try adding an Accept header to your request?
也许您可以尝试在您的请求中添加一个Accept标头? Maybe curl adds that automatically.
也许 curl 会自动添加。
Also there's a -k in your curl request telling it to ignore SSL validation, which I'm not sure if it affects the .NET code.在您的 curl 请求中还有一个 -k 告诉它忽略 SSL 验证,我不确定它是否会影响 .NET 代码。 In other words, does curl still work without the '-k'?
换句话说,如果没有“-k”,curl 是否仍然有效? Then, no worries.
那么,不用担心。 Otherwise, perhaps you need to tell .NET to also ignore SSL validation.
否则,也许您需要告诉 .NET 也忽略 SSL 验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.