locked
remote server returned an error: (500) Internal Server Error. RRS feed

  • Question

  • User766819745 posted

    Hi Frnds, I have Using HTTP Post Method ,Already create Webconfig fine

    (  <webServices>
          <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
          </protocols>
        </webServices>

    )

    But i got  remote server returned an error: (500) Internal Server Error. i am not using SOAP..

     public void Post()
            {
                Uri serverUrl = new Uri(_targetURL);

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(serverUrl); //"http://localhost:1445/eProcurement.NET.Web/OrderConf.text.aspx"

                webRequest.Method = "POST";

                UTF8Encoding encoding = new UTF8Encoding();

                //ASCIIEncoding

                if ((_value != null) || (_postMessage != null))
                {

                    byte[] postBytes = null;
                    Stream postStream = null;
                    HttpWebResponse webResponse = null;
                    Stream responseStream = null;


                    string xmlBuffer = "";
                    byte[] stringBuffer = new byte[50 * 1024];
                    byte[] buffer = new byte[1024];
                    int stringSize = 0;
                    int count = 0;

                    if (_value != null)
                    {
                        webRequest.ContentType = _contentType;

                        postBytes = encoding.GetBytes(_value);

                        webRequest.ContentLength = postBytes.Length;

                        //webRequest.ContentLength = postBytes.Length;

                        try
                        {
                            postStream = webRequest.GetRequestStream();

                            postStream.Write(postBytes, 0, postBytes.Length);
                            Console.WriteLine("postBytes.Length " + postBytes.Length);
                            postStream.Close();

                            webResponse = (HttpWebResponse)webRequest.GetResponse();
                            Console.WriteLine("webResponse = (HttpWebResponse)webRequest.GetResponse();" + postBytes.Length);
                            responseStream = webResponse.GetResponseStream();
                            Console.WriteLine("responseStream = webResponse.GetResponseStream();" + postBytes.Length);

                            xmlBuffer = "";

                            stringSize = 0;

                            count = 0;


                            while ((count = responseStream.Read(buffer, 0, 1024)) > 0)
                            {
                                Array.Copy(buffer, 0, stringBuffer, stringSize, count);
                                stringSize += count;
                            }
                            xmlBuffer = System.Text.UTF8Encoding.UTF8.GetString(stringBuffer, 0, stringSize);

                            responseStream.Close();

                            webResponse.Close();

                            _response = xmlBuffer;

                        }
                        catch (WebException wex)
                        {
                            if (wex.Response != null)
                            {
                                using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
                                {
                                    Console.WriteLine("The server returned '{0}' with the status code {1} ({2:d}).", errorResponse.StatusDescription, errorResponse.StatusCode, errorResponse.StatusCode);
                                }
                            }

                        }

                        //catch (WebException ex)
                        //{
                        //    Console.WriteLine(ex.Message);
                        //    //throw new Exception(ex.Message);
                        //}

                        //catch (Exception ex)
                        //{
                        //    Console.WriteLine(ex.Message);
                        //    //throw new Exception(ex.Message);
                        //}
                    }
                    else
                    {
                        webRequest.ContentType = "text";

                        postBytes = encoding.GetBytes(_postMessage);

                        webRequest.ContentLength = postBytes.Length;

                        try
                        {
                            postStream = webRequest.GetRequestStream();

                            postStream.Write(postBytes, 0, postBytes.Length);

                            postStream.Close();

                            webResponse = (HttpWebResponse)webRequest.GetResponse();

                            responseStream = webResponse.GetResponseStream();


                            xmlBuffer = "";

                            stringSize = 0;

                            count = 0;


                            while ((count = responseStream.Read(buffer, 0, 1024)) > 0)
                            {
                                Array.Copy(buffer, 0, stringBuffer, stringSize, count);
                                stringSize += count;
                            }
                            xmlBuffer = System.Text.UTF8Encoding.UTF8.GetString(stringBuffer, 0, stringSize);

                            responseStream.Close();

                            webResponse.Close();

                            _response = xmlBuffer;

                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            //throw new Exception(ex.Message);
                        }
                    }
                }
                else
                {
                    //webResponse.Close();
                
                }



                /*        String Buffer="";
                        PrintWriter pw = null;

                            URL ServerURL = new URL(targetURL);

                            //Create new URLConnection object from the URL
                            HttpURLConnection URLCon = (HttpURLConnection)ServerURL.openConnection();

                            URLCon.setDoInput(true);
                            URLCon.setUseCaches(false);

                            if(postValueList != null || postMessage != null)
                            {
                            URLCon.setDoOutput(true);
                            URLCon.setRequestMethod("POST");

                                pw = new PrintWriter(URLCon.getOutputStream());

                                if(postValueList != null)
                                {

                                    for(int i=0; i< postValueList.size() ; i++)
                                    {
                                        if(i > 0)
                                            pw.print("&");

                                    PostValue CurPostValue=(PostValue)(postValueList.elementAt(i));
                                    pw.print(CurPostValue.parm + "=" + URLEncoder.encode(CurPostValue.value) );

                                    }

                                }
                                else
                                {
                                    pw.print(postMessage);
                                }

                                pw.close();

                        }
                        else
                        {
                            URLCon.setDoOutput(false);
                        }

                //            System.out.println(URLCon.getContentType());

                            BufferedReader in = new BufferedReader(
                                        new InputStreamReader(
                                        URLCon.getInputStream()));

                            String inputLine;

                            while ((inputLine = in.readLine()) != null)
                                Buffer += inputLine + "\n";

                            in.close();

                            response=Buffer;

                */
            }

    Saturday, September 20, 2008 4:47 AM

Answers

  • User1071970124 posted

    What do you expect anyone to do with this mess? Do you expect someone to do your job for you?

    Part of your job is to narrow down problems. You should either narrow them down sufficiently for you to solve them yourself, or at least enough that someone else can help you. This means, in part, that you must remove aspects of your code that do not contribute to the problem. This can be as easy as deleting code until the problem goes away, then adding code until the problem returns.

    At the very least, it means not just copying and pasting all of your code. You could at least have removed the parts that are commented out! [:@]

    You could also have posted the complete exception that you receive. To catch and display an exception:

     

    try
    {
        // Failing code here
    }
    catch (Exception ex)
    {
        string exceptionString = ex.ToString();
        // Display it with Console.WriteLine(exceptionString) or something else
        throw;   // Never eat exceptions unless you mean it
    }
    

      

    Help us help you - don't just expect us to do all the work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 20, 2008 8:37 AM