Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The remote server returned an error: (500) Internal Server Error at
C#
System.Net.HttpWebRequest.GetResponse()


Working fine on local machine but create issue after testing the application on server

below is my code

C#
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();
}


My MVC3 application code which receive image byte array

C#
[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;    
            }
        }
Posted
Comments
manoj2289 25-Jul-14 8:46am    
Server Error in '/CareCredit' Application.

The remote server returned an error: (500) Internal Server Error.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Source Error:


Line 63: }
Line 64: //Gets the response
Line 65: WebResponse response = req.GetResponse();
Line 66: //Writes the Response
Line 67: Stream responseStream = response.GetResponseStream();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900