Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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

>Solution :

Well, this part is one problem:

 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());
  

You’re calling ToString ( data.ToString() ) on a Byte[]. Not sure what you’re expecting, but the output is going to be literally the string "System.Byte[]" Did you mean to convert it to base 64 or something?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading