title: The remote server returned an error: (500) Internal Server Error on server at System.Net.HttpWebRequest.GetResponse()
body: <p>The remote server returned an error: (500) Internal Server Error
   at <code>System.Net.HttpWebRequest.GetResponse()</code></p>
  
  <p>Working fine on local machine but create issue after testing the application on server</p>
  
  <p>below is my code</p>
  
  <pre><code>foreach (var file in pt1_upload)
                  {
                      MemoryStream target = new MemoryStream();
                      file.InputStream.CopyTo(target);
                      byte[] byteArray = target.ToArray();
                      string ServiceUrl = ConfigurationManager.AppSettings["ServiceUrl"].ToString();
                      WebRequest request = WebRequest.Create(ServiceUrl + "image/UploadPhotoForService?tabNo=1");
                      request.Method = "POST";
                      request.ContentType = "application/x-www-form-urlencoded";
                      request.ContentLength = byteArray.Length;
                      Stream dataStream = request.GetRequestStream();
                      dataStream.Write(byteArray, 0, byteArray.Length);
                      dataStream.Close();
                      WebResponse response = request.GetResponse();
                      dataStream = response.GetResponseStream();
                      StreamReader reader = new StreamReader(dataStream);
                      string responseFromServer = reader.ReadToEnd();
                      Console.WriteLine(responseFromServer);
                      reader.Close();
                      dataStream.Close();
                      response.Close();
  }
  </code></pre>
  
  <p>My MVC3 application code which receive image byte array</p>
  
  <pre><code>[HttpPost]
          [System.Web.Services.WebMethod]
          public string UploadPhotoForService(int tabNo)
          {
              try
              {
                      byte[] buffer = new byte[Request.InputStream.Length];
                      Request.InputStream.Read(buffer, 0, buffer.Length);
                      MemoryStream ms = new MemoryStream(buffer);
                      Image returnImage = Image.FromStream(ms);
                      var fileName = Guid.NewGuid().ToString();
  
                      var myPath = "~/TempImages/";
  
                      if (!Directory.Exists(Server.MapPath(myPath)))
                      {
                          DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(myPath));
                      }
                      var physicalPath = Path.Combine(Server.MapPath(myPath), fileName + ".jpeg");
                      returnImage.Save(physicalPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  
              }
              catch (Exception ex)
              {
                  return ex.Message;    
              }
          }
  </code></pre>