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