繁体   English   中英

在Web应用程序中使用类时,出现字符不清晰的HttpWebResponse

[英]I get HttpWebResponse with unclear characters when use a class in web application

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

此类从登录之前和登录之后的某个页面获取响应,我将分析这些页面。

任何时候都一切正常。

我在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
}

和我的例外:

登录异常
解析HTML时出错
索引超出范围。 必须为非负数并且小于集合的大小。
参数名称:索引

您正在接受gzip并压缩压缩的响应(通过Accept-Encoding ),但是您没有检查响应是否被压缩。 开启自动解压缩功能可为您完成此操作:

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

暂无
暂无

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

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