Search code examples
c#responsewebrequest

Problem with getting response of a HTTP Web request


You can see my code down there.This is going to show user id of a instagram user as the response but it is showing "�".

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;
    }

Solution

  • It's answered in a comment by @Dour, but I'm going to add a bit of detail that isn't mentioned.

    This following line tells the server: Hey server! Please send me a compressed response (to reduce size).

    httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
    

    so, the response you're getting is a compressed response.

    Does that mean you just remove this line ?

    Well, Removing it will fix your problem but that isn't the correct way to do that.

    It's really better to get a reduced size response because that will need lower time to download it from the server.

    What you really need to do is to make HttpWebRequest handles the decompression process for you.

    You can do that by setting AutomaticDecompression property.

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

    Note: With the previous line, you do NOT need to set Accept-Encoding yourself. HttpWebRequest will send it automatically for you. And when you call GetResponse, HttpWebRequest will handle the decompression process for you.


    Besides of your problem: I suggest that you use using statement for getting the response, because with your current code, I think it won't get disposed.