简体   繁体   English

调试HttpWebResponse

[英]Debugging an HttpWebResponse

This is being done on a Windows Forms App. 这是在Windows Forms应用程序上完成的。 I've spent a ton of time stepping through this code with the debugger. 我花了很多时间在调试器中逐步完成此代码。 What I've found are the following things and they all seem to be at this line: 我发现了以下内容,它们似乎都在这一行:

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

1. If I include 1.如果我包括

request.SendChunked = true;

I get this error at the response line previously stated: 我在前面所述的响应行中收到此错误:

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

2. If I comment out the code in #1, I receive this error at that main response line that I mentioned in the beginning: 2.如果我注释掉#1中的代码,则会在开始提到的主要响应行中收到此错误:

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

3. If I go with route #1, the "Connection" of the request remains as "KeepAlive" all the way through. 3.如果我使用路由#1,则请求的“连接”始终保持为“ KeepAlive”。 But if I go with route #2, the "Connection" of the request changes to "null" at the response line that I mentioned in the beginning. 但是,如果我使用路由#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);

        }
    }

I feel like these things are adding up towards a solution, but I could really use some guidance. 我觉得这些东西正逐步形成一个解决方案,但我确实可以使用一些指导。

Option 1: Change your content type to match the body encoding 选项1:更改内容类型以匹配正文编码

request.ContentType = "application/xml";

Option 2: Change your body encoding to match the specified content-type 选项2:更改您的身体编码以匹配指定的内容类型

If your server expects only "application/x-www-form-urlencoded", then you need to change your body encoding to suit it, for example, like this: 如果您的服务器只希望使用“ 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);
    }

You need to know the parameter name (above was set to "arg") and add a reference to System.Web, if you haven't. 您需要知道参数名称(以上设置为“ arg”),并添加对System.Web的引用。

See following XML... 请参阅以下XML ...

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

and encoded string for reference (your request body should look similar to this): 和编码的字符串以供参考(您的请求主体应与此类似):

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

Explanation 说明

If you look at the response you are getting with the first approach: 415 - Unsupported Media Type , you can notice that the content type you are specifying ( "application/x-www-form-urlencoded" ) doesn't match what you are sending in the body (an XML document). 如果您以第一种方法查看响应,请执行以下操作: 415-不支持的媒体类型 ,您可能会注意到您指定的内容类型( “ application / x-www-form-urlencoded” )与您的内容不匹配在正文中发送(一个XML文档)。 Chunk encoding should be enabled when sending files. 发送文件时应启用块编码。

Note 注意

When you are having trouble with a request done in source code, try to test the request alone with a web debugging tool, like Fiddler . 如果您在用源代码完成请求时遇到麻烦,请尝试使用Web调试工具(例如Fiddler)单独测试该请求。 There you would compose and issue the request until you get the response you want. 在那里,您将撰写并发出请求,直到获得所需的响应为止。 Then you can compare that with what you are sending from source code (again you should use the same tool for inspecting your request). 然后,您可以将其与源代码发送的内容进行比较(再次,您应使用相同的工具检查您的请求)。

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

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