gpt4 book ai didi

c# - 在 Web 应用程序中使用类时,我得到带有不明确字符的 HttpWebResponse

转载 作者:行者123 更新时间:2023-11-30 21:38:40 25 4
gpt4 key购买 nike

我在使用 HttpWebResponse 的 Windows 窗体应用程序中创建了一个类。

这个类在登录前和登录后获取一些页面的响应,我分析这些页面。

一切都很好。

但是

我在 Web 应用程序中使用了此类,并在服务器中上传了项目文件。

除了登录页面,我从所有页面都得到了很好的响应。登录页面响应包含不明确的字符,如下所示:

�      �Z�N#G�v��CM�'��X؎2�LvV�e�H{��݅����t�
(ڋa���z.��2�͒H��膇
�+�9U�v�n��$pW��S�|��s�)=z����ߗ����0*T��~JuF5y�Ǟ���D��

而且这种情况经常发生,这意味着我有时会得到正常的回应。

例如,当在服务器中再次提取我的 zip 文件时,它可能会正常工作。

我很困惑,我分不清正则。

URL = "http://www.nonsense-website.com/login";
var request2 = (HttpWebRequest)WebRequest.Create(URL);

string username1 = jandoe;
string password1 = pass1234;

string postData = "hidlogin=1&username=" + username1 + "&password=" + password1;

var data = Encoding.UTF8.GetBytes(postData);

request2.Method = "POST";
request2.ContentType = "application/x-www-form-urlencoded";
request2.ContentLength = data.Length;
request2.UserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:55.0) Gecko/20100101 Firefox/55.0";
request2.Headers.Add("Cookie:" + cookie);
request2.Headers.Add("Accept-Language: en-US,en;q=0.5");
request2.Headers.Add("Accept-Encoding: gzip, deflate");
request2.KeepAlive = true;
request2.Headers.Add("Upgrade-Insecure-Requests: 1");

using (var stream = request2.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}

try
{
using (var response2 = (HttpWebResponse)request2.GetResponse())
{
responseString = new StreamReader(response2.GetResponseStream(),Encoding.UTF8).ReadToEnd();
AnalyzeResponse(responseString); // other function that analyze response data
}
}
catch (Exception ex)
{
WriteData("Exception in Login" + Environment.NewLine + ex.Message); // write in file
}

还有我的异常(exception):

Exception in Login
Error in Parse the HTML
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

最佳答案

您正在接受 gzip 和 deflate 压缩响应(通过 Accept-Encoding),但您没有检查响应是否被压缩。打开自动解压,为您完成:

request2.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

关于c# - 在 Web 应用程序中使用类时,我得到带有不明确字符的 HttpWebResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45776739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com