简体   繁体   English

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

[英]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.

相关问题 为什么我的 Web api 在使用 WebClient 类时返回状态代码 401,而在使用 HttpWebResponse 时返回状态代码 200? - Why my web api return Status code 401 when i use WebClient class and Status code 200 when i use HttpWebResponse? 使用httpWebResponse时我似乎无法退回任何东西 - I cannot seem to get anything back when using httpWebResponse 如何获得“200 OK”的HttpWebResponse? - How do I get an HttpWebResponse for “200 OK”? 从Web应用程序调用时如何获取类库的路径? - How to get path of class library when calling from a web application? HttpWebResponse和编码(奇怪的字符) - HttpWebResponse and encoding (weird characters) HttpWebResponse剥离换行符 - HttpWebResponse stripping newline characters 以编程方式将数据发布到Web表单时出现乱码的httpWebResponse字符串 - Garbled httpWebResponse string when posting data to web form programmatically 在C#桌面应用程序中从HttpWebResponse获取JSESSIONID - Get JSESSIONID from HttpWebResponse in C# desktop application 当我运行appRoleAssignments时,它返回不清楚的响应 - when i run appRoleAssignments, it returns the unclear response 在多个线程中使用时,HttpWebResponse变得混乱 - HttpWebResponse get mixed up when used inside multiple threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM