简体   繁体   English

C#HttpWebRequest POST奇怪的响应

[英]C# HttpWebRequest POST strange response

Good morning, I am writing a program that makes a POST webrequest to a website (websta.me). 早上好,我正在编写一个对网站(websta.me)进行POST webrequest的程序。 The request works, but the problem is the response. 该请求有效,但问题是响应。 I analyzed the http requests using Charles and this is the normal response: 我使用Charles分析了http请求,这是正常的响应:

{"meta":{"code":200},"data":{"created_time":"xs","text":"xxxx <3","from":{"username":"xxxx","profile_picture":"xxxx","id":"xxxx","full_name":"xxxx"},"id":"xxxx"},"status":"OK","called_count":x}

My program is returning this: 我的程序返回此:

但是我的程序正在返回这个

Any ideas on how to solve this? 关于如何解决这个问题的任何想法? Thanks in advance for any help :) This is my code if it can help: 预先感谢您的帮助:)这是我的代码,如果可以帮助您:

byte[] bytes = ASCIIEncoding.UTF8.GetBytes("comment=+xxx&media_id=xxx");
HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("http://websta.me/api/comments/xxx);
WebHeaderCollection postHeaders = postReq.Headers;
postReq.Method = "POST";
postReq.Timeout = 10000;
postReq.Host = "websta.me";
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/35.0";
postReq.Accept = "application/json, text/javascript, */*; q=0.01";
postHeaders.Add("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3");
postHeaders.Add("Accept-Encoding", "gzip, deflate");
postReq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
postHeaders.Add("X-Requested-With", "XMLHttpRequest");
postReq.Referer = "http://websta.me/feed";
postReq.ContentLength = bytes.Length;

postReq.CookieContainer = cookies;

postReq.KeepAlive = true;

postHeaders.Add("Pragma", "no-cache");
postHeaders.Add("Cache-Control", "no-cache");
Stream postStream = postReq.GetRequestStream();
postStream.Write(bytes, 0, bytes.Length);
postStream.Close();
HttpWebResponse postResponse;
postResponse = (HttpWebResponse)postReq.GetResponse();
StreamReader reader = new StreamReader(postResponse.GetResponseStream());
MessageBox.Show(reader.ReadToEnd());

The result you are receiving from your request is GZip compressed data (the first three bytes are 0x1f8b08 ). 您从请求中收到的结果是GZip压缩数据(前三个字节是0x1f8b08 )。

To get the text from it, just decompress it using GZipStream . 要从中获取文本,只需使用GZipStream解压缩。 Something like this: 像这样:

private byte[] Decompress(byte[] data)
{
    using (GZipStream stream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress))
    {
        const int size = 4096;
        byte[] buffer = new byte[size];
        using (MemoryStream memory = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = stream.Read(buffer, 0, size);
                if (count > 0)
                {
                    memory.Write(buffer, 0, count);
                }
            }
            while (count > 0);
            return memory.ToArray();
        }
    }
}

Then modify the end of your code (from where you get your response) to: 然后将代码的结尾(从您的响应处)修改为:

postResponse = (HttpWebResponse)postReq.GetResponse();
Stream Reader = postResponse.GetResponseStream();
byte[] Data = new byte[postResponse.ContentLength];
Reader.Read(Data, 0, Data.Length);
Data = Decompress(Data);
string Result = System.Text.Encoding.UTF8.GetString(Data);
MessageBox.Show(Result);

This reads the response data (which is compressed) into a byte array, then decompresses it before converting it to the appropriate text (which I believe is utf-8 from the content-type description). 这会将响应数据(已压缩)读取到一个字节数组中,然后在将其转换为适当的文本之前将其解压缩(我相信这是内容类型描述中的utf-8)。

Please note that the GZip decompression routine is taken from http://www.dotnetperls.com/decompress (it is not mine). 请注意,GZip解压缩例程来自http://www.dotnetperls.com/decompress (不是我的)。

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

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