繁体   English   中英

调试HttpWebResponse

[英]Debugging an HttpWebResponse

这是在Windows Forms应用程序上完成的。 我花了很多时间在调试器中逐步完成此代码。 我发现了以下内容,它们似乎都在这一行:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

1.如果我包括

request.SendChunked = true;

我在前面所述的响应行中收到此错误:

'System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type.

2.如果我注释掉#1中的代码,则会在开始提到的主要响应行中收到此错误:

'System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.

3.如果我使用路由#1,则请求的“连接”始终保持为“ KeepAlive”。 但是,如果我使用路由#2,则请求的“连接”在开始时提到的响应行上将变为“空”。

    private void HttpPost()
    {


        HttpWebRequest request = null;
        Uri uri = new Uri("https://post.craigslist.org/bulk-rss/post");

        request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        XmlDocument doc = new XmlDocument();
        doc.Load("XMLFile1.xml");
        //request.ContentLength = doc.InnerXml.Length;

        request.SendChunked = true;
        using (Stream writeStream = request.GetRequestStream())
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] bytes = encoding.GetBytes(doc.InnerXml);
            //request.ContentLength = bytes.Length;
            writeStream.Write(bytes, 0, bytes.Length);
        }


        string result = string.Empty;

        request.ProtocolVersion = System.Net.HttpVersion.Version11;
        request.KeepAlive = false;
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }

            }

        }
        catch (Exception e)
        {
            string innerException = String.Format("Inner exception: '{0}'", e.Data);
            string exceptionCause = String.Format("An error occurred: '{0}'", e);
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\DebugOutputFile\exception.txt", exceptionCause);
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\DebugOutputFile\innerException.txt", innerException);

        }
    }

我觉得这些东西正逐步形成一个解决方案,但我确实可以使用一些指导。

选项1:更改内容类型以匹配正文编码

request.ContentType = "application/xml";

选项2:更改您的身体编码以匹配指定的内容类型

如果您的服务器只希望使用“ application / x-www-form-urlencoded”,那么您需要更改主体编码以适合它,例如,如下所示:

    using (Stream writeStream = request.GetRequestStream())
    {
        UTF8Encoding encoding = new UTF8Encoding();
        string response = String.Concat("arg=", HttpUtility.UrlEncode(doc.InnerXml))
        byte[] bytes = encoding.GetBytes(doc.InnerXml);
        //request.ContentLength = bytes.Length;
        writeStream.Write(bytes, 0, bytes.Length);
    }

您需要知道参数名称(以上设置为“ arg”),并添加对System.Web的引用。

请参阅以下XML ...

<?xml version="1.0" encoding="UTF-8"?><test></test>

和编码的字符串以供参考(您的请求主体应与此类似):

arg=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%3Ctest%3E%3C%2Ftest%3E

说明

如果您以第一种方法查看响应,请执行以下操作: 415-不支持的媒体类型 ,您可能会注意到您指定的内容类型( “ application / x-www-form-urlencoded” )与您的内容不匹配在正文中发送(一个XML文档)。 发送文件时应启用块编码。

注意

如果您在用源代码完成请求时遇到麻烦,请尝试使用Web调试工具(例如Fiddler)单独测试该请求。 在那里,您将撰写并发出请求,直到获得所需的响应为止。 然后,您可以将其与源代码发送的内容进行比较(再次,您应使用相同的工具检查您的请求)。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM