读取JavaScript文件并通过HttpHandler将其输出

时间:2011-10-15 08:13:00

标签: c# .net asp.net twitter httphandler

你知道Twitter没有通过https提供其小部件javascript文件,所以我决定通过httphandler提供它,到目前为止,我迷路了!

这是我到目前为止所做的:

public void ProcessRequest(HttpContext context) {

    context.Response.ContentType = "application/javascript";

    WebRequest request = WebRequest.Create("http://widgets.twimg.com/j/2/widget.js");
    request.Method = "GET";
    request.ContentType = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

    using (WebResponse response = request.GetResponse()) {

        using (Stream requestStream = response.GetResponseStream()) {

            Stream outStream = context.Response.OutputStream;
            byte[] buffer = new byte[1024];
            int len = (int)response.ContentLength, bytes;

            while (len > 0 && (bytes =
                requestStream.Read(buffer, 0, buffer.Length)) > 0) {

                outStream.Write(buffer, 0, bytes);
                len -= bytes;
            }
        }
    }
}

我没有收到任何错误但是当我调试代码时,我看到response.ContentLength将以-1的形式返回。我在这里缺少什么?

更新

尝试下面的代码,看它是否有任何区别,但也没有用:

public void ProcessRequest(HttpContext context) {

    context.Response.ContentType = "application/x-javascript";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://widgets.twimg.com/j/2/widget.js");
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    request.Method = "GET";

    using (WebResponse response = request.GetResponse()) {

        using (Stream requestStream = response.GetResponseStream()) {

            Stream outStream = context.Response.OutputStream;
            byte[] buffer = new byte[1024];
            int len = (int)response.ContentLength, bytes;

            while (len > 0 && (bytes =
                requestStream.Read(buffer, 0, buffer.Length)) > 0) {

                outStream.Write(buffer, 0, bytes);
                len -= bytes;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    public override void ProcessRequest(HttpContext context)
    {
        int bytesProcessed = 0;
        Stream remoteStream = null;
        Stream localStream = null;
        context.Response.ContentType = "application/octet-stream";

        WebRequest request = WebRequest.Create("http://widgets.twimg.com/j/2/widget.js");
    //    request.Method = "GET";
        request.ContentType = "application/octet-stream";

        using (WebResponse response = request.GetResponse())
        {

            using (Stream requestStream = response.GetResponseStream())
            {
                localStream = File.Create(@"c:\1.y2yy");

                // Allocate a 1k buffer
                byte[] buffer = new byte[1024];
                int bytesRead;

                // Simple do/while loop to read from stream until
                // no bytes are returned
                do
                {
                    // Read data (up to 1k) from the stream
                    bytesRead = requestStream.Read(buffer, 0, buffer.Length);

                    // Write the data to the local file
                    localStream.Write(buffer, 0, bytesRead);

                    // Increment total bytes processed
                    bytesProcessed += bytesRead;
                } while (bytesRead > 0);
                localStream.Close();
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

答案 1 :(得分:1)

我用下面的代码解决了我的问题。不确定这是否是正确的做法:

    public void ProcessRequest(HttpContext context) {

        context.Response.ContentType = "application/x-javascript";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://widgets.twimg.com/j/2/widget.js");
        request.Accept = "application/javascript";
        request.KeepAlive = false;
        request.Method = "GET";

        HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse();

        Encoding enc = System.Text.Encoding.GetEncoding(1252);
        StreamReader loResponseStream = new
          StreamReader(webresponse.GetResponseStream(), enc);

        string Response = loResponseStream.ReadToEnd();

        context.Response.Write(Response);
    }

答案 2 :(得分:0)

你的代码没有问题,只是你总是假设编码是1252.所以如果你知道它是没有问题的。在完美的世界中,服务器的响应将指示其标头中的编码。但情况并非总是如此。有关该主题的更多信息,请查看此question

我改变了你的代码。

 public void ProcessRequest(HttpContext context)
 {           
        WebRequest request = WebRequest.Create("http://widgets.twimg.com/j/2/widget.js");
        request.Method = "GET";
        request.ContentType = "application/javascript";

        using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
        {
            Encoding enc = String.IsNullOrEmpty(response.CharacterSet) ? 
                Encoding.GetEncoding(1252) : Encoding.GetEncoding(response.CharacterSet);

            using (var reader = new StreamReader(response.GetResponseStream(), enc))
            {
                var jsContent = reader.ReadToEnd();

                if (!String.IsNullOrEmpty(jsContent)) {
                    context.Response.ContentType = "application/javascript";
                    context.Response.Write(jsContent);
                }                    
            }
        }
 }