title: Unable to Pass Image in HTTP POST in a HTTP Web Request in ASP.NET
body: <p>I am having trouble converting an image into bytes and saving it in database.</p>
  
  <p>Here is the description,</p>
  
  <p>I have an image that will be send from a remote device to the web server using HTTP POST.</p>
  
  <p>so what I am doing is I ask them to sent the image to me.</p>
  
  <p>Since the data is sent in POST i assume they will send me the Bytes by converting the bytes into string using </p>
  
  <p>byte[] img = FileUpload1.FileBytes;
  Encoding enc = Encoding.ASCII;
  string img = enc.GetString(img);</p>
  
  <p>Then they make a WebRequest using HTTPWebRequest and append this image in HTTP POST.</p>
  
  <p>The Whole Code for making the request ---</p>
  
  <pre><code>    WebRequest request = WebRequest.Create(url);
      request.Method = "POST";
  
  
      //Create the POST Data
  
      FileUpload img = (FileUpload)imgUpload;
      Byte[] imgByte = null;
      if (img.HasFile &amp;&amp; img.PostedFile != null)
      {
          imgByte = imgUpload.FileBytes;
      }
      string imgPh = null;
      Encoding enc = Encoding.ASCII;
      if (imgByte != null)
      {
          imgPh = enc.GetString(imgByte);
      }
      string postData = "sid=8062BD53EB4552AD6D0FBB7E5DC5B7AF&amp;status=Y&amp;uid=123456789012&amp;fname=Dinesh Singh&amp;lname=Malik&amp;ftname=Balwan&amp;yrbirth=1988&amp;gender=Male&amp;address1=Address1&amp;address2=Address2&amp;address3=Address3&amp;address4=Address4&amp;imagePh=" + imgPh;
  
  
      byte[] post = Encoding.UTF8.GetBytes(postData);
  
  
      //Set the Content Type
      request.ContentType = "application/x-www-form-urlencoded";
      request.ContentLength = post.Length;
  
      Stream reqdataStream = request.GetRequestStream();
      // Write the data to the request stream.
      reqdataStream.Write(post, 0, post.Length);
  
      reqdataStream.Close();
  
      // If required by the server, set the credentials.
      request.Credentials = CredentialCache.DefaultCredentials;
      WebResponse response = null;
      try
      {
          // Get the response.
          response = request.GetResponse();
  
      }
      catch (Exception ex)
      {
          Response.Write("Error Occured.");
      }
  </code></pre>
  
  <p>On the Page to which Request is made I am getting this image again into bytes using</p>
  
  <pre><code>    Encoding enc = Encoding.ASCII;
  
      byte[] imagePhoto = enc.GetBytes(postData["imageph"]);
  </code></pre>
  
  <p>From Here I save it into my Database</p>
  
  <p>But when I retrieve the image using the Handler, it does not show the image.</p>
  
  <hr>
  
  <p>The issue is the conversion of the image from byte[] to string and then converting string into byte[] at the Web Server. (Because when I save the image directly without this conversion using TestPage on server it shows the image.)</p>
  
  <p>So What am I doing wrong in this.</p>
  
  <p><strong>Also is there any way in the above code to get the data of HTTP Post as received by the Web Server (retrieve HTTP Headers).</strong>
  I want to retrieve this data received to sent it back to the Other Development to develop the request at the device in the same format as I am receiving in the HTTP Web Request URL</p>
  
  <p>Any help would be appreciated.</p>