0

I need to send this line of HTTP request as bytes. What is the correct way?

client.write(byte(" <? xml version: '1.0' encoding='utf-8'?><VideoOverlay><normalizedScreenSize><normalizedScreenWidth>704</normalizedScreenWidth><normalizedScreenHeight>576</normalizedScreenHeight></normalizedScreenSize><attribute><transparent>false</transparent><flashing>false</flashing></attribute><fontSize>adaptive</fontSize><frontColorMode>auto</frontColorMode><alignment>customize</alignment><TextOverlayList><TextOverlay><id>1</id><enabled>true</enabled><displayText>dsadasdasdas</displayText><positionX>0</positionX><positionY>480</positionY></TextOverlay></TextOverlayList><DateTimeOverlay><enabled>true</enabled><positionY>544</positionY><positionX>0</positionX><dateStyle>MM-DD-YYYY</dateStyle><timeStyle>24hour</timeStyle><displayWeek>true</displayWeek></DateTimeOverlay><channelNameOverlay><enabled>true</enabled><positionY>64</positionY><positionX>16</positionX></channelNameOverlay></VideoOverlay>"));

To be more clear this is my C# code for the request

byte[] data = encoder.GetBytes(" <? xml version: '1.0' encoding='utf-8'?><VideoOverlay><normalizedScreenSize><normalizedScreenWidth>704</normalizedScreenWidth><normalizedScreenHeight>576</normalizedScreenHeight></normalizedScreenSize><attribute><transparent>false</transparent><flashing>false</flashing></attribute><fontSize>adaptive</fontSize><frontColorMode>auto</frontColorMode><alignment>customize</alignment><TextOverlayList><TextOverlay><id>1</id><enabled>true</enabled><displayText>" + textBox1.Text + "</displayText><positionX>0</positionX><positionY>480</positionY></TextOverlay></TextOverlayList><DateTimeOverlay><enabled>true</enabled><positionY>544</positionY><positionX>0</positionX><dateStyle>MM-DD-YYYY</dateStyle><timeStyle>24hour</timeStyle><displayWeek>true</displayWeek></DateTimeOverlay><channelNameOverlay><enabled>true</enabled><positionY>64</positionY><positionX>16</positionX></channelNameOverlay></VideoOverlay>");

This is my full C# working code

 ServicePointManager.Expect100Continue = false;
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] data = encoder.GetBytes(" <? xml version: '1.0' encoding='utf-8'?><VideoOverlay><normalizedScreenSize><normalizedScreenWidth>704</normalizedScreenWidth><normalizedScreenHeight>576</normalizedScreenHeight></normalizedScreenSize><attribute><transparent>false</transparent><flashing>false</flashing></attribute><fontSize>adaptive</fontSize><frontColorMode>auto</frontColorMode><alignment>customize</alignment><TextOverlayList><TextOverlay><id>1</id><enabled>true</enabled><displayText>" + textBox1.Text + "</displayText><positionX>0</positionX><positionY>480</positionY></TextOverlay></TextOverlayList><DateTimeOverlay><enabled>true</enabled><positionY>544</positionY><positionX>0</positionX><dateStyle>MM-DD-YYYY</dateStyle><timeStyle>24hour</timeStyle><displayWeek>true</displayWeek></DateTimeOverlay><channelNameOverlay><enabled>true</enabled><positionY>64</positionY><positionX>16</positionX></channelNameOverlay></VideoOverlay>");
        Uri a = new Uri("http://192.168.0.64");
        HttpWebRequest request = WebRequest.Create("http://192.168.0.64/ISAPI/System/Video/inputs/channels/1/overlays") as HttpWebRequest;
        request.Method = "PUT";
        request.Credentials = new NetworkCredential("admin", "12345qwerty");
        request.ContentLength = 888;
        request.ContentType = "application / x - www - form - urlencoded; charset = UTF - 8";
        request.ContentLength = data.Length;
        request.GetRequestStream().Write(data, 0, data.Length);
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        response.Close();

This is the Arduino version

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 177 };
byte server[] = { 192,168, 0, 64}; // Google
String big_string="<?xml versiding='rlay>";
EthernetClient client;

void setup()
{
  Ethernet.begin(ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("POST /ISAPI/System/Video/inputs/channels/1/overlays HTTP/1.0");
    client.println("Host: 192.168.0.64");
    client.println("User-Agent: arduino-ethernet");
    client.println("Authorization: Basic YWRtaW46MTIzNDVxd2VydHk=");
   client.println("Content-Type: application / x - www - form - urlencoded; charset = UTF - 8");

    client.println("content-length:888");
    // client.println();
    //client.println(big_string.length());
    client.write(" <? xml version: '1.0' encoding='utf-8'?><VideoOverlay><normalizedScreenSize><normalizedScreenWidth>704</normalizedScreenWidth><normalizedScreenHeight>576</normalizedScreenHeight></normalizedScreenSize><attribute><transparent>false</transparent><flashing>false</flashing></attribute><fontSize>adaptive</fontSize><frontColorMode>auto</frontColorMode><alignment>customize</alignment><TextOverlayList><TextOverlay><id>1</id><enabled>true</enabled><displayText>dsadasdasdas</displayText><positionX>0</positionX><positionY>480</positionY></TextOverlay></TextOverlayList><DateTimeOverlay><enabled>true</enabled><positionY>544</positionY><positionX>0</positionX><dateStyle>MM-DD-YYYY</dateStyle><timeStyle>24hour</timeStyle><displayWeek>true</displayWeek></DateTimeOverlay><channelNameOverlay><enabled>true</enabled><positionY>64</positionY><positionX>16</positionX></channelNameOverlay></VideoOverlay>");

     //client.println();
    //client.print(big_string);
    // client.println();






  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {

    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}
11
  • why do you think that you have to send the request as bytes? – jsotola Nov 23 '18 at 8:28
  • Because i have a c# aplication that works with sending the request as bytes , in order for the ip camera to read them , and now i need to do the same for the arduino – Ivanh23 Nov 23 '18 at 8:42
  • why do you think that a string character, such as an x is not a byte? – jsotola Nov 23 '18 at 8:51
  • 1
    remove the trailing space from " <xml. in C# version you send correct Content-length. In arduino you send 888. the type must by without spaces application/x-www-form-urlencoded – Juraj Nov 23 '18 at 10:32
  • 1
    For one it's <?xml version='1.0' encoding='utf-8'?> and "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"``. Secondly you specify the content-length to be 888, while the string above is 895 characters long. Thirdy the data you send isn't actually application/x-www-form-urlencoded`, but most likely the server doesn't care about this. Lastly, you haven't told us what the actual problem is. What doesn't work? Where does the C# code come into play here? – Gerben Nov 23 '18 at 17:01
0

Characters are bytes. ASCII codes of characters are the values. There is no difference between client.print(str) and client.write(str, strlen(str)). And client.write(str) is the same too.

In C# or Java the constant string is of type String. It is an object. You must call a method of the object to get the raw char/byte array. In Arduino a constant string is of type const char* a.k.a. const char[] which is on low level the same as const byte* or const byte[]

Equivalent of C#

byte[] data = encoder.GetBytes("some string");

for ASCII characters (not UTF) in C is

byte data[] = "some string";

9
  • I read that (client.write) send bytes and (client.pritnln) sends string and chars etc , i understand your answer. but i think it needs to be like my c# code that i edited in my question – Ivanh23 Nov 23 '18 at 9:07
  • in C# the constant string is of type String. it is an object. You must call a function to get the char/byte array. in Arduino a constant string is of type const char* a.k.a. const char[] which is on low level the same as const byte* or const byte[] – Juraj Nov 23 '18 at 9:11
  • And what would be the way to do this ? sorry i am not that good in programming and i am having a little hard time with the bytes array – Ivanh23 Nov 23 '18 at 9:58
  • without byte(. it does wrong – Juraj Nov 23 '18 at 10:02
  • One more question what about the Json library ? can it be used for this problem ? – Ivanh23 Nov 23 '18 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.