c# - 读取 JavaScript 文件并通过 HttpHandler 将其输出回来

标签 c# .net asp.net twitter httphandler

你知道 Twitter 不通过 h​​ttps 提供其小部件 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;
            }
        }
    }
}

最佳答案

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)
    {

    }
}

关于c# - 读取 JavaScript 文件并通过 HttpHandler 将其输出回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7776631/

相关文章:

c# - 在标签中显示 ListView 行

c# - 当我使用打开文件选择器打开 RichEditBox UWP C# 的文本文件时访问被拒绝

c# - 什么是 IEqualityComparer<double> 的实现 GetHashCode()

jquery - 数据库更新后立即更新 UI 的最佳方法是什么(ASP.NET 和 JQuery)

c# - 使用 Unity 设置依赖注入(inject)以将类的实例传递给构造函数

ASP.NET 2.5 前缀 ctl00 和 ASP.NET 4 不前缀 ctl00

c# - 如何使光线转换与物理管理器中的图层发生碰撞

c# - 将 List<T> 绑定(bind)到 WinForm 中的 DataGridView

.net - .NET 中具有超过 2^31 个键的大数组的选项

c# - 如何在 C# 中删除字符串中的前 n 行?