0

I am trying to use the HttpWebRequest for posting a byteArray at C#. i dont want to convert the bytes to string (or to ToBase64String). i just want to send the byteArray to the server as is. let say that my data is

byte[] byteArray = { 0, 1, 5, 4, 0, 1, 55, 0, 1, 5, 4, 0, 1}

what should i define at the post parameters?

request.ContentType = "application/x-www-form-urlencoded"??? or request.ContentType = "application/octet-stream";???

I think I am missing something big....(BTW, the server is WampServer (Koana,PHP,MySQL)

Thanks!

2 Answers 2

1

An easier way to do this would be to use a WebClient:

byte[] byteArray = { 0, 1, 5, 4, 0, 1, 55, 0, 1, 5, 4, 0, 1};

using(WebClient wc = new WebClient())
{
    wc.UploadData(someURL, byteArray);
}

For binary data you usually would use application/octet-stream - but this is not required (depends on the server), i.e. the WebClient upload above does not specify a content-type header.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, please see my answer below.
0
 byte[] byteArray = { 0, 1, 5, 4, 0, 1, 55, 0, 1, 5, 4, 0, 1}

 using (Stream requestStream = httpWebRequest.GetRequestStream()) {
     if (requestStream.CanWrite)
         requestStream.Write(byteArray, 0, postBytes.Length);
 }

3 Comments

i think the issue is on the server side, for both ways i am getting that the $_POST is empty ( even though i can see the data at the fiddler - ��7��...). i am not sure where should i check... any suggestion?
Are you including something like the following when you create your HttpWebRequest? httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "POST";` httpRequest.ContentType = "application/x-www-form-urlencoded";` httpRequest.Timeout = 5000;
sure, the request is working fine for a string with/without the Convert.ToBase64String. I can see the string (row data) at the server DB. the thing is that when i am trying to send the binary data (by application/x-www-form-urlencoded or by application/octet-stream) the php server varibale $_POST is empty. i am not sure where it's being set at the server, at the request class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.