2

You know Twitter is not serving its widget javascript file over https so I decided to serve it through httphandler and so far, I am lost!

Here is what I have done so far :

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;
            }
        }
    }
}

I am getting no error but when I debug the code, I saw that response.ContentLength is coming back as -1. What am I missing here?

UPDATE

Tried the below code as well to see if it makes any difference and didn't work either:

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;
            }
        }
    }
}
tugberk
  • 57,477
  • 67
  • 243
  • 335

3 Answers3

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

    }
}
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • StatusCode and StatusDescription is also coming as OK. – tugberk Oct 15 '11 at 08:23
  • also for request.ContentType ? – Royi Namir Oct 15 '11 at 08:24
  • I already used it for request.ContentType. Doesn't matter what I use for context.Response.ContentType, response ContentLength is coming back as -1. – tugberk Oct 15 '11 at 08:28
  • Thanks for your help. I solved the problem with different approach : http://stackoverflow.com/questions/7776631/reading-a-javascript-file-and-outputing-it-back-through-httphandler/7776787#7776787 – tugberk Oct 15 '11 at 08:49
1

I solved my problem with the below code. not sure if it is the right way of doing it :

    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);
    }
tugberk
  • 57,477
  • 67
  • 243
  • 335
0

There is no problem with your code except that you always assume the encoding is 1252. So if you know it is there is no problem. In a perfect world the response of the server will indicate the encoding in its headers. But that is not always the case. Check out this question for more info on that subject.

I altered your code a litte bit.

 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);
                }                    
            }
        }
 }
Community
  • 1
  • 1
Martijn B
  • 4,065
  • 2
  • 29
  • 41