简体   繁体   English

发布数据时Webresponse上的内部服务器错误,但可以使用浏览器C#进行工作

[英]Internal server error on webresponse while posting data but works using a browser C#

I am trying to post a form using HttpWebRequest but get an internal server error. 我正在尝试使用HttpWebRequest发布表单,但出现内部服务器错误。 I have tried setting useragent in the request but no success. 我尝试在请求中设置useragent,但未成功。

Here is the url which has got the form. 这是具有表格的网址。

https://portal.llg.de/tracking/xml/sender.asp?cust=anonym_ https://portal.llg.de/tracking/xml/sender.asp?cust=anonym_

and this is my sample code 这是我的示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace postdata
{
    public class RequestManager
    {
        public static string LastResponse { protected set; get; }


        public static void Main(string[] args)
        {
            System.Net.ServicePointManager.Expect100Continue = false;
             string content = "XML=<VORGANG><KUNDE>6400320</KUNDE><LIEFADR>..... Tokio, Japan</LIEFADR><POS NUMMER='1'><ARTNR>9000032</ARTNR><MENGE>10</MENGE><BESTAND>25</BESTAND></POS><POS NUMMER='2'><ARTNR>9161161</ARTNR><MENGE>100</MENGE><BESTAND>33</BESTAND><TERMIN>20050401</TERMIN><PREIS>80.00</PREIS></POS><VERSAND>80.00</VERSAND></VORGANG>";
             string url = "https://portal.llg.de/tracking/xml/receiver.asp?cust=_anonym_";

           var response =  SendPOSTRequest(url, content, true);
           var responsecontent = GetResponseContent(response);

        }

        public static string GetResponseContent(HttpWebResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            Stream dataStream = null;
            StreamReader reader = null;
            string responseFromServer = null;

            try
            {
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                reader = new StreamReader(dataStream);
                // Read the content.
                responseFromServer = reader.ReadToEnd();
                // Cleanup the streams and the response.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {                
                if (reader != null)
                {
                    reader.Close();
                }
                if (dataStream != null)
                {
                    dataStream.Close();
                }
                response.Close();
            }
            LastResponse = responseFromServer;
            return responseFromServer;
        }

        public static HttpWebResponse SendPOSTRequest(string uri, string content,  bool allowAutoRedirect)
        {
            HttpWebRequest request = GeneratePOSTRequest(uri, content,  allowAutoRedirect);
            return GetResponse(request);
        }

        public static HttpWebRequest GeneratePOSTRequest(string uri, string content, bool allowAutoRedirect)
        {
            return GenerateRequest(uri, content, "POST", null, null, allowAutoRedirect);
        }

        internal static HttpWebRequest GenerateRequest(string uri, string content, string method, string login, string password, bool allowAutoRedirect)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            // Create a request using a URL that can receive a post. 
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            // Set the Method property of the request to POST.
            request.Method = method;
            // Set cookie container to maintain cookies
        //    request.CookieContainer = cookies;
            request.AllowAutoRedirect = false;
            // If login is empty use defaul credentials
            if (string.IsNullOrEmpty(login))
            {
              //  request.Credentials = CredentialCache.DefaultNetworkCredentials;
                request.Credentials = CredentialCache.DefaultCredentials; 
            }
            else
            {
                request.Credentials = new NetworkCredential(login, password);
            }
            if (method == "POST")
            {
                // Convert POST data to a byte array.
                byte[] byteArray = Encoding.UTF8.GetBytes(content);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";

                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
            }
            return request;
        }

        internal static HttpWebResponse GetResponse(HttpWebRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();                
            }
            catch (WebException ex)
            {
                Console.WriteLine("Web exception occurred. Status code: {0}", ex.Status);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return response;
        }

    }
}

I debugged with fiddler both posts. 我用提琴手调试了两个帖子。 Something is weird in the form post. 表单发布中有些奇怪。 The xml conatains "+" between the tags. xml在标记之间包含“ +”。

If you use this string the post works. 如果使用此字符串,则该帖子有效。

 content = "XML=<VORGANG><KUNDE>6400320</KUNDE><LIEFADR>.....+Tokio,+Japan</LIEFADR><POS+NUMMER=\"1\">++<ARTNR>9000032</ARTNR>++<MENGE>10</MENGE>++<BESTAND>25</BESTAND></POS><POS+NUMMER=\"2\">++<ARTNR>9161161</ARTNR>++<MENGE>100</MENGE>++<BESTAND>33</BESTAND>++<TERMIN>20050401</TERMIN>++<PREIS>80.00</PREIS></POS><VERSAND>80.00</VERSAND></VORGANG>";

Fiddler Debug: 提琴手调试:

POST https://portal.llg.de/tracking/xml/receiver.asp?cust=_anonym_ HTTP/1.1
Host: portal.llg.de
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Referer: https://portal.llg.de/tracking/xml/sender.asp?cust=anonym_
Cookie: ASPSESSIONIDSQSACSTQ=CIGFABLBPHPMLBDOPJIKPHJB
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 588

XML=%3CVORGANG%3E%0D%0A%3CKUNDE%3E6400320%3C%2FKUNDE%3E%0D%0A%3CLIEFADR%3E.....+Tokio%2C+Japan%3C%2FLIEFADR%3E%0D%0A%3CPOS+NUMMER%3D%221%22%3E%0D%0A++%3CARTNR%3E9000032%3C%2FARTNR%3E%0D%0A++%3CMENGE%3E10%3C%2FMENGE%3E%0D%0A++%3CBESTAND%3E25%3C%2FBESTAND%3E%0D%0A%3C%2FPOS%3E%0D%0A%3CPOS+NUMMER%3D%222%22%3E%0D%0A++%3CARTNR%3E9161161%3C%2FARTNR%3E%0D%0A++%3CMENGE%3E100%3C%2FMENGE%3E%0D%0A++%3CBESTAND%3E33%3C%2FBESTAND%3E%0D%0A++%3CTERMIN%3E20050401%3C%2FTERMIN%3E%0D%0A++%3CPREIS%3E80.00%3C%2FPREIS%3E%0D%0A%3C%2FPOS%3E%0D%0A%3CVERSAND%3E80.00%3C%2FVERSAND%3E%0D%0A%3C%2FVORGANG%3E%0D%0A

if you use url decode to decode the xml="..:" 如果您使用网址解码来解码xml =“ ..:”

you get this: 你得到这个:

XML=<VORGANG>
<KUNDE>6400320</KUNDE>
<LIEFADR>.....+Tokio,+Japan</LIEFADR>
<POS+NUMMER="1">
++<ARTNR>9000032</ARTNR>
++<MENGE>10</MENGE>
++<BESTAND>25</BESTAND>
</POS>
<POS+NUMMER="2">
++<ARTNR>9161161</ARTNR>
++<MENGE>100</MENGE>
++<BESTAND>33</BESTAND>
++<TERMIN>20050401</TERMIN>
++<PREIS>80.00</PREIS>
</POS>
<VERSAND>80.00</VERSAND>
</VORGANG>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WEbResponse引发500内部服务器错误 - WEbResponse throws 500 Internal server error 获取服务器错误 500,“内部服务器错误”,但在 Postman 上工作正常,在 c# 中使用 httpclient - getting server error 500, 'Internal Server Error', but works fine on Postman, using httpclient in c# 使用servicestack和c#将数据发布到sql server - Posting data to sql server using servicestack and c# 使用 HTTPClient 将字典作为数据发布到 C# 中的 HTTPS 服务器 - Posting a dictionary as data to HTTPS server in C# using HTTPClient 使用 MS Graph 站点 API 和 C# 时出现内部服务器错误 - Internal Server Error while using MS Graph Site API with C# 出现错误:远程服务器返回错误:(401)未经授权。 ,同时将我的网站评论发布到Twitter。 使用C# - Getting error : The remote server returned an error: (401) Unauthorized. , while posting my website comments to twitter. Using C# C#内部服务器错误 - C# Internal Server Error 从Windows服务将数据发布到Web应用程序时出错。“远程服务器返回错误:(500)内部服务器错误。” - Error While posting Data to a web application from a windows service.“The remote server returned an error: (500) Internal Server Error.” 使用C#时出现服务器错误 - Getting Server error while using c# 如何获取服务器的IP地址响应WebResponse C# - How to Get IP Address of Server responding to WebResponse C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM