'C# - Can't get this seemingly simple web request working
I have the following powershell Web-Request that works correctly for sending a command to a PTZ camera:
Invoke-WebRequest -UseBasicParsing -Uri "http://192.168.111.75/ajaxcom" `
-Method "POST" `
-Headers @{
"Accept"="application/json, text/javascript, */*; q=0.01"
"Accept-Encoding"="gzip, deflate"
"Accept-Language"="en-US,en;q=0.9"
"DNT"="1"
"Origin"="http://192.168.111.75"
"X-Requested-With"="XMLHttpRequest"
} `
-ContentType "application/x-www-form-urlencoded; charset=UTF-8" `
-Body "szCmd=encodedStringHere"
I'm trying to recreate this in C# but I can't seem to get it to work..
I've tried a whole bunch of stuff but this is what my code looks like right now:
public async static void Execute()
{
using HttpClient client = new HttpClient();
// client.DefaultRequestHeaders.Accept.Add(
// new MediaTypeWithQualityHeaderValue("application/json, text/javascript, */*; q=0.01"));
// client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip, deflate"));
// client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US,en;q=0.9"));
client.DefaultRequestHeaders.Add("DNT", "1");
client.DefaultRequestHeaders.Add("Origin", "http://192.168.111.75");
client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
string stringData = "{\"SysCtrl\":{\"PtzCtrl\":{\"nChanel\":0,\"szPtzCmd\":\"right_start\",\"byValue\":50}}}";
var data = Encoding.Default.GetBytes(stringData);
KeyValuePair<string, string> kvp = new KeyValuePair<string, string>("Body", data.ToString());
var content = new FormUrlEncodedContent(new[] { kvp });
var response = await client.PostAsync("http://192.168.111.75/ajaxcom", content);
var responseString = await response.Content.ReadAsStringAsync();
}
I don't know why I'm having so much trouble with this, any help is appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
