WebRequest等效于CURL命令
我正试图将一个正在运行的curl命令转换为ac#WebRequest。
我已阅读了不少post,我很确定我的代码是正确的,但它仍然无效。
任何人都可以看到我做错了吗?
这是工作curl命令:
curl -k -ux: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响应:
The remote server returned an error: (406) Not Acceptable.
任何帮助将非常感激!
谢谢
根据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(streamReader.ReadToEnd()); return answer; } }
这是我用来发布json数据的curl命令:
curl http://IP:PORT/my/path/to/endpoint -H 'Content-type:application/json' -d '[{...json data...}]'
这相当于上面用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); }
这是我将json数据发布到使用API调用或web服务的解决方案
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 } }
根据这个关于406的问题: 什么是HTTP中的“406-Not Accepled Response”? 也许您可以尝试在您的请求中添加Accept标头? 也许curl会自动添加。
你的curl请求中还有-k告诉它忽略SSLvalidation,我不确定它是否会影响.NET代码。 换句话说,如果没有’-k’,curl是否仍然可以工作? 然后,不用担心。 否则,您可能需要告诉.NET也忽略SSLvalidation。