简体   繁体   English

如何在C#中使用HttpWebRequest读取JavaScript文件

[英]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. 我在远程服务器上有一个javascript文件,当我使用httpwebrequest时,它返回一些奇怪的字符。

Thr url is http://goo.gl/0Ug5QI 网址是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. 查看链接到的js源,可能是gzip压缩了。 Try saving the source as a file and use 7zip or something to see if you can unzip it. 尝试将源文件另存为文件,并使用7zip或其他方式查看是否可以将其解压缩。 There is a GZip library in C# so if it has been gzipped then you should be able to unzip it easily enough. C#中有一个GZip库,因此如果已将其压缩,则应该可以轻松地将其解压缩。

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. 无论哪种方式,您发布的代码都不成问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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