[英]I get HttpWebResponse with unclear characters when use a class in web application
I created a class in Windows Form application that uses HttpWebResponse
. 我在Windows窗体应用程序中使用HttpWebResponse
创建了一个类。
This class gets the response from some page before login and after login and I analyze this pages. 此类从登录之前和登录之后的某个页面获取响应,我将分析这些页面。
Everything is good working at all time. 任何时候都一切正常。
BUT 但
I used this class in a web application and uploaded project files in server. 我在Web应用程序中使用了此类,并在服务器中上传了项目文件。
I get good response from all page EXCEPT login page . 我从除登录页面之外的所有页面都得到了很好的响应。 Login page response contains unclear characters like this: 登录页面响应包含不清楚的字符,例如:
� �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��
and it happens often , that means I get sometimes normal response. 而且它经常发生,这意味着我有时会得到正常反应。
For example when extract my zip file again in server, it may work fine. 例如,当再次在服务器中提取我的zip文件时,它可能工作正常。
I am confused, I can't tell regular rule. 我很困惑,我不能说规则。
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
}
and my exception: 和我的例外:
Exception in Login 登录异常
Error in Parse the HTML 解析HTML时出错
Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。
Parameter name: index 参数名称:索引
You're accepting gzip and deflate compressed responses (via Accept-Encoding
), but you aren't checking whether the response is compressed. 您正在接受gzip并压缩压缩的响应(通过Accept-Encoding
),但是您没有检查响应是否被压缩。 Turn on automatic decompression to have this done for you: 开启自动解压缩功能可为您完成此操作:
request2.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.