views:

55

answers:

1

I'm currently sending image data over as a byte array. However, I wish to send it over as ASCII string. How can I send an ASCII string from client to server using HTTP POST in c#?

    HttpWebRequest webRequest = null; 
    webRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.1.2/"); 
    webRequest.ContentType = "application/x-www-form-urlencoded";                                            
    webRequest.Method = "POST";    

//Assume here, I got the ascii string from image using ImageToBase64() function.

byte[] buffer = encoding.GetBytes(myString)); //assume myString is an ASCII string

    // Set content length of our data 
    webRequest.ContentLength = buffer.Length; 

    webStream = webRequest.GetRequestStream(); 
    webStream.Write(buffer, 0, buffer.Length); //This is the only way I know how to send data -using byte array "buffer". But i wan to send data over as ASCII string "myString". How?
    webStream.Close(); 

    //I used this function to turn an image into ASCII string to be sent
    public string ImageToBase64(Image image, 
      System.Drawing.Imaging.ImageFormat format)
    {
      using (MemoryStream ms = new MemoryStream())
      {
        // Convert Image to byte[]
        image.Save(ms, format);
        byte[] imageBytes = ms.ToArray();

        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);
        return base64String;
      }
    }
A: 

I suggest ditching HttpWebRequest in favor of WebClient. Also if you are sending using the content-type of application/x-www-form-urlencoded, then the server will be expecting the posted data to match the format of key1=value1&key2=value2.

// sending image to server as base 64 string
string imageBase64String = "YTM0NZomIzI2OTsmIzM0NTueYQ...";
using (WebClient client = new WebClient())
{
    var values = new NameValueCollection();
    values.Add("image", imageBase64String);
    client.UploadValues("http://192.168.1.2/", values);
} 

Same thing, but using WebHttpRequest

string imageBase64String = "YTM0NZomIzI2OTsmIzM0NTueYQ...";
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.1.2/");
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
string formPostString = "image=" + HttpUtility.UrlEncode(imageBase64String);
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(formPostString);
webRequest.ContentLength = buffer.Length;
using (Stream webStream = webRequest.GetRequestStream())
{
    webStream.Write(buffer, 0, buffer.Length);
}

Then, you can handling the sent image on the server-side using the HttpRequest object.

string imageBase64String = HttpContext.Current.Request.Form["image"];
jojaba
[WebClient.UploadValues](http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx) is even better for `"application/x-www-form-urlencoded"` payload as it sets the header and does the URL encoding for you.
dtb
@dtb: Good tip to know. Thanks.
jojaba
The thing is I need to get a response from the server. Therefore I used WebReuqest. I've been instructed to use HTTP Post and send ASCII string using HTTP Post, therefore how can that be done? What should the "ContentType" be? I'm not familiar with what is ContentType actually so I just put "application/x-www-form-urlencoded"
Robogal
@robogal: Content-Type simply defines what type of data is being POST'ed. Without a Content-Type, the server handling the POST will not understand what kind of data is being sent. `application/x-www-form-urlencoded` is the most common format for send POST'ed data over the internet. Almost every website that has a form, uses this type.
jojaba
Is it possible to use HttpWebRequest to HTTP POST an string over? Instead of using WebClient? SO far I only know how to HTTP POST a byte[] over..
Robogal
@robogal: I just edited the answer to include an example for HttpWebRequest and WebClient.
jojaba