Debugging an HttpWebResponse

本文关键字:HttpWebResponse an Debugging | 更新日期: 2023-09-27 18:12:38

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

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,请求的"Connection"在我开始提到的响应行更改为"null"。

    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);
        }
    }

我觉得这些事情加起来是一个解决方案,但我真的需要一些指导。

Debugging an HttpWebResponse

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

request.ContentType = "application/xml";

选项2:更改body编码以匹配指定的content-type

如果您的服务器只期望"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的引用。网页,如果你没有。

请看下面的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-网址-form-urlencoded")与您在正文(XML文档)中发送的内容不匹配。在发送文件时应该启用块编码。

注意

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