Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have written a code like this :-

C#
static void PostData()
        {
            
            try
            {
                // Create a http request to the server endpoint that will pick up the
                // file and file description.
                HttpWebRequest requestToServerEndpoint = (HttpWebRequest)WebRequest.Create("http://192.168.4.226:80/era/applications?appid=909_99");             
                          

                string boundaryString = String.Format("----------{0:N}", Guid.NewGuid());
                string fileUrl = @"C:\Users\filename-1.0.par";
                // Set the http request header \\
                requestToServerEndpoint.Method = WebRequestMethods.Http.Post;
                requestToServerEndpoint.ContentType = "multipart/form-data; boundary=" + boundaryString;
                requestToServerEndpoint.KeepAlive = true;
                NetworkCredential nc = new NetworkCredential("admin", "admin");
                requestToServerEndpoint.Credentials = nc;

                MemoryStream postDataStream = new MemoryStream();
                StreamWriter postDataWriter = new StreamWriter(postDataStream);

                // Include value from the myFileDescription text area in the post data
                //postDataWriter.Write("\r\n--" + boundaryString + "\r\n");
                //postDataWriter.Write("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}",
                // "package.par", "package.par");

                // Include the file in the post data
  postDataWriter.Write("\r\n--" + boundaryString + "\r\n");
                postDataWriter.Write("Content-Disposition: form-data;"
                                        + "name=\"{0}\";"
                                        + "filename=\"{1}\""
                                        + "\r\nContent-Type: {2}\r\n\r\n",
                                        "package.par", Path.GetFileName(fileUrl),
                                        Path.GetExtension(fileUrl));
                postDataWriter.Flush();

                //read

                FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    postDataStream.Write(buffer, 0, bytesRead);
                }
                fileStream.Close();
                //////////
              postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
                postDataWriter.Flush();

                // Set the http request body content length
                requestToServerEndpoint.ContentLength = postDataStream.Length;

                // Dump the post data from the memory stream to the request stream
                using (Stream s = requestToServerEndpoint.GetRequestStream())
                {
                    postDataStream.WriteTo(s);
                }
                postDataStream.Close();

                //////response
              bool b=  requestToServerEndpoint.HaveResponse;
                
                WebResponse response = requestToServerEndpoint.GetResponse();
                StreamReader responseReader = new StreamReader(response.GetResponseStream());
                string replyFromServer = responseReader.ReadToEnd();
                HttpWebResponse response2 = (HttpWebResponse)requestToServerEndpoint.GetResponse();
                string returnString = response2.StatusCode.ToString();
                System.Console.WriteLine("response=" + returnString);
                if (returnString.Equals("OK"))
                {
                    System.Console.WriteLine("Request Successful");
                }
                else
                {
                    System.Console.WriteLine("Request Unsuccessful");
                }
            }
            catch (WebException wex)
            {
                Stream c = wex.Response.GetResponseStream();

                System.Console.WriteLine("Exception=" + wex.Message);

                // Exception message and stack trace
                Console.WriteLine(wex.Message);
                Console.WriteLine(wex.StackTrace);

                // Inspect the reason of communication failure
                switch (wex.Status)
                {
                    case WebExceptionStatus.ConnectFailure:
                        // handle failure to connect to server
                        break;

                    case WebExceptionStatus.ReceiveFailure:
                        // handle failure to receive complete
                        // HTTP response from server
                        break;

                    case WebExceptionStatus.Timeout:
                        // handle timeout when waiting for
                        // HTTP response from server
                        break;

                    case WebExceptionStatus.ConnectionClosed:
                        // handle connection with server closed
                        // prematurely
                        break;

                    // This is where we can examine HTTP status
                    // codes other than 2xx and the server response
                    // body
                    case WebExceptionStatus.ProtocolError:
                        // Examine the HTTP response returned
                        HttpWebResponse response = (HttpWebResponse)wex.Response;
                        StreamReader responseReader =
                            new StreamReader(response.GetResponseStream());
                        string responseFromServer = responseReader.ReadToEnd();
                        switch (response.StatusCode)
                        {
                            case HttpStatusCode.BadRequest:
                                // handle bad request (http status code 400)
                                break;
                            case HttpStatusCode.InternalServerError:
                                // handle internal server error (http status code 500)
                                break;
                            default:
                                // handle other http status code returned by
                                // the server.
                                break;
                        } // end switch(res.StatusCode)
                        response.Close();
                        break;

                    case WebExceptionStatus.NameResolutionFailure:
                        // handle DNS error
                        break;

                    default:
                        // handle other errors not in this switch statement
                        break;
                }
            }
        }


getting error internal server error 500
Posted
Updated 16-May-13 18:45pm
v3
Comments
Thanks7872 17-May-13 0:47am    
Refer to this link.Internal Server Error 500

1 solution

The server says by this error that i cannot complete the request because i encountered an error. Further you can refer to this link for brief introduction regarding the error codes(including 5xx errors).

The HTTP status code in IIS 7.0, IIS 7.5, and IIS 8.0
 
Share this answer
 
v2

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