![](/img/trans.png)
[英]Why my web api return Status code 401 when i use WebClient class and Status code 200 when i use 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.