获取 HTTP Web 请求的响应时出现问题
Problem with getting response of a HTTP Web request
你可以看到我的代码 there.This 将显示 instagram 用户的用户 ID 作为响应,但它显示“�”。
private void button18_Click(object sender, EventArgs e)
{
string username = ""; // your username
string password = ""; // your password
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://i.instagram.com/api/v1/accounts/login/");
httpWebRequest.Headers.Add("X-IG-Connection-Type", "WiFi");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
httpWebRequest.Headers.Add("X-IG-Capabilities", "AQ==");
httpWebRequest.Accept = "*/*";
httpWebRequest.UserAgent = "Instagram 10.9.0 Android (23/6.0.1; 944dpi; 915x1824; samsung; SM-T185; gts210velte; qcom; en_GB)";
httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
httpWebRequest.Headers.Add("Accept-Language", "en;q=1, ru;q=0.9, ar;q=0.8");
httpWebRequest.Headers.Add("Cookie", "mid=qzejldb8ph9eipis9e6nrd1n457b;csrftoken=a7nd2ov9nbxgqy473aonahi58y21i8ee");
httpWebRequest.Host = "i.instagram.com";
httpWebRequest.KeepAlive = true;
byte[] bytes = new ASCIIEncoding().GetBytes("ig_sig_key_version=5&signed_body=5128c31533802ff7962073bb1ebfa9972cfe3fd9c5e3bd71fe68be1d02aa92c8.%7B%22username%22%3A%22"+ username + "%22%2C%22password%22%3A%22"+ password +"%22%2C%22_uuid%22%3A%22D26E6E86-BDB7-41BE-8688-1D60DE60DAF6%22%2C%22_uid%22%3A%22%22%2C%22device_id%22%3A%22android-a4d01b84202d6018%22%2C%22_csrftoken%22%3A%22a7nd2ov9nbxgqy473aonahi58y21i8ee%22%2C%22login_attempt_count%22%3A%220%22%7D");
httpWebRequest.ContentLength = (long)bytes.Length;
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
string result = new StreamReader(((HttpWebResponse)httpWebRequest.GetResponse()).GetResponseStream()).ReadToEnd();
textBox1.Text = result;
}
@Dour 在评论中对此进行了回答,但我将添加一些未提及的细节。
下面这行告诉服务器:嘿服务器!请给我发送压缩回复(以减小大小)。
httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
所以,您得到的响应是压缩响应。
这是否意味着您只需删除此行?
嗯,删除它会解决您的问题,但这不是正确的方法。
获得较小尺寸的响应确实更好,因为从服务器下载响应所需的时间更少。
您真正需要做的是让HttpWebRequest
为您处理解压过程。
您可以通过设置 AutomaticDecompression
属性.
来做到这一点
httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
注意: 对于上一行,您不需要自己设置 Accept-Encoding
。 HttpWebRequest
会自动为您发送。当您调用GetResponse
时,HttpWebRequest
会为您处理解压过程。
除了你的问题:
我建议您使用 using
语句来获取响应,因为使用您当前的代码,我认为它不会被处置。
你可以看到我的代码 there.This 将显示 instagram 用户的用户 ID 作为响应,但它显示“�”。
private void button18_Click(object sender, EventArgs e)
{
string username = ""; // your username
string password = ""; // your password
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://i.instagram.com/api/v1/accounts/login/");
httpWebRequest.Headers.Add("X-IG-Connection-Type", "WiFi");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
httpWebRequest.Headers.Add("X-IG-Capabilities", "AQ==");
httpWebRequest.Accept = "*/*";
httpWebRequest.UserAgent = "Instagram 10.9.0 Android (23/6.0.1; 944dpi; 915x1824; samsung; SM-T185; gts210velte; qcom; en_GB)";
httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
httpWebRequest.Headers.Add("Accept-Language", "en;q=1, ru;q=0.9, ar;q=0.8");
httpWebRequest.Headers.Add("Cookie", "mid=qzejldb8ph9eipis9e6nrd1n457b;csrftoken=a7nd2ov9nbxgqy473aonahi58y21i8ee");
httpWebRequest.Host = "i.instagram.com";
httpWebRequest.KeepAlive = true;
byte[] bytes = new ASCIIEncoding().GetBytes("ig_sig_key_version=5&signed_body=5128c31533802ff7962073bb1ebfa9972cfe3fd9c5e3bd71fe68be1d02aa92c8.%7B%22username%22%3A%22"+ username + "%22%2C%22password%22%3A%22"+ password +"%22%2C%22_uuid%22%3A%22D26E6E86-BDB7-41BE-8688-1D60DE60DAF6%22%2C%22_uid%22%3A%22%22%2C%22device_id%22%3A%22android-a4d01b84202d6018%22%2C%22_csrftoken%22%3A%22a7nd2ov9nbxgqy473aonahi58y21i8ee%22%2C%22login_attempt_count%22%3A%220%22%7D");
httpWebRequest.ContentLength = (long)bytes.Length;
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
string result = new StreamReader(((HttpWebResponse)httpWebRequest.GetResponse()).GetResponseStream()).ReadToEnd();
textBox1.Text = result;
}
@Dour 在评论中对此进行了回答,但我将添加一些未提及的细节。
下面这行告诉服务器:嘿服务器!请给我发送压缩回复(以减小大小)。
httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
所以,您得到的响应是压缩响应。
这是否意味着您只需删除此行?
嗯,删除它会解决您的问题,但这不是正确的方法。
获得较小尺寸的响应确实更好,因为从服务器下载响应所需的时间更少。
您真正需要做的是让HttpWebRequest
为您处理解压过程。
您可以通过设置 AutomaticDecompression
属性.
httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
注意: 对于上一行,您不需要自己设置 Accept-Encoding
。 HttpWebRequest
会自动为您发送。当您调用GetResponse
时,HttpWebRequest
会为您处理解压过程。
除了你的问题:
我建议您使用 using
语句来获取响应,因为使用您当前的代码,我认为它不会被处置。