Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I've been looking over this for a few hours now and I cant see whats going wrong. I've used PostMan to create a GET request to the same link and it actually returned some nicely parsed HTML data. When using PostMan I am using the exact same headers as when I do it with code but for some reason when I do it with code it returns gibberish.

"\u001f�\b\0\0\0\0\0\0\u0003�R�n�0\u0010��+�{�ZԲ,7m\u0012C�m�I\0���\b\u0002�\\EL%R W��ǿ��m 9� qf�Ŭv�x���˷\u001f�W�p\u05eeO��\0\U000d02be/QHi\a�\u001e'E�~}\u0002P4$�\u0004\u0002�\u0005�F8O\\��uz��+���TVi�J|�秧p@\u007f��R��7����@\nI\u0004�\u07b2��\u0004�\aTd�ॣ\u0011\u001d�����[�\b�\u001a&\u0013:xҊ�R�VKJ#��6��hS\u001fܨ��\u0019tb���{)\r�\\�\n���ߨ�'h\u001c�%fG����Ak�{��2�T�����6��z.[;�څ��8;��$?V��>\vӠ�< ���\u0016%z�\f��6\u0010\u001e[�\r\u0011\a��]��=�𣑠�&\a����~<Z?\u0006gߧ���ى~]d{�W\u0013~H�5qw�@^\u0015�J�(�?\u0018��F\\��\ng\u0018�I\u001eW?�o���E���&�x7��7��\u0017� �'�/����|���Kb��c��ʪq�dZt�r�!��>�ό�\u001aȹ\u0002\0\0"


However.. PostMan returns this.
[Visual representation]


Here is the code that makes the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/en/login");

            request.Method = "GET";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36";
            request.Headers.Add("Accept-Language", "sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7");


            var loginResp = request.GetResponse();
            string cont = null;

            Stream fAnswer = loginResp.GetResponseStream();
            StreamReader sr = new StreamReader(fAnswer, Encoding.UTF8);

            //This right here is what returns the gibberish.
            cont = sr.ReadToEnd();


What I have tried:

I've tried playing around with the encoding settings & changing headers.
Posted
Updated 19-Dec-17 22:30pm
Comments
Jochen Arndt 20-Dec-17 4:26am    
It looks like the respond is compressed. Try it without the "Accept-Encoding" header.

1 solution

You accept gzip and deflate responses, so this "gibberish" is actually the compressed response. That's not a problem though, you can tell HttpWebRequest to automatically decompress it:
C#
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

I see you also accept "br", and I couldn't find auto-decompression for that, so it's probably a good idea to remove "br" from your accepted encodings, unless you want to take care of the decompression yourself if you happen to get a "br" response.
 
Share this answer
 
Comments
aleksvarga 20-Dec-17 4:54am    
Oh wow! Thank you so much!
What about cookies? It's not returning any cookies at all but on PostMan it does.
https://ghostbin.com/paste/rz7ss
Thomas Daniels 20-Dec-17 5:43am    
I can reproduce not receiving a cookie, but what is the cookie that PostMan gets? Without seeing it, I have no clue why .Cookies is empty.
aleksvarga 20-Dec-17 5:47am    
The cookie it gets is some sort of csrf_Token
https://i.imgur.com/xxj4vi3.png
Thomas Daniels 20-Dec-17 5:54am    
Ah. The purpose of such a cookie is to avoid Cross-Site Request Forgery (you can look that up for more details). Anyway, I have no idea why it's not in .Cookies... but it's still possible to retrieve it using the headers: response.Headers[HttpResponseHeader.SetCookie].
aleksvarga 20-Dec-17 5:57am    
Oh yeah! That actually makes sense and thats very smart!
So something like this?
string cookies = response.Headers[HttpResponseHeader.SetCookie];
Because SetCookie sets the headers? And since it should only include 1 we dont need to itterate through it? Please correct my mistakes if there was any in there becase I am still trying to learn.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900