gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:01 24 4
gpt4 key购买 nike

你知道 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com