简体   繁体   中英

How to read javascript file using HttpWebRequest in c#

I have a javascript file in a remote server, and when I use httpwebrequest it returns some weird characters.

Thr url is http://goo.gl/0Ug5QI

Is this kind of compressed contents?

static string GetScriptSource(string _url)
{
    string _retValue = string.Empty;

    HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(_url);
    hwr.Method  = "GET";

    HttpWebResponse res = (HttpWebResponse)hwr.GetResponse();
    StreamReader sr = new StreamReader(res.GetResponseStream());

    return sr.ReadToEnd();
}

My code to read that script file's content is very simple.

Looking at the js source that you linked to, it could be that is has been gzipped. Try saving the source as a file and use 7zip or something to see if you can unzip it. There is a GZip library in C# so if it has been gzipped then you should be able to unzip it easily enough.

Although it's a Korean web site so maybe the encoding is not correct.

Either way it's not a problem with the code that you posted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM