httpWebRequest / Javascript


T

Thomas Goodson

Does anybody here have an example of using the httpWebRequest object in
ASP/JavaScript (um...JScript)? I need the basic ability to retrieve a web
page (most likely requested via a POST) into a string variable.
** Tom **
 
Ad

Advertisements

P

Peter Bromberg [C# MVP]

HttpWebRequest is a .NET Framework class and represents server-side code. I
think, as Bruce pointed out, that what you want for Javascript (client side
code) is the XmlHttpRequest object.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: htp://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
 
T

Thomas Goodson

Ok, I've seen that page, but it applies to client side usage of the XHR
object and not server side use of the httpWebRequest object.
** Tom **
 
T

Thomas Goodson

NOPE! I do what little ASP programming that I do in JavaScript (yes on the
server side.) Bruce's suggestion was great and I use that code on the client
side, but I need to build a proxy on the server to allow access to content
that is not in my domain. (there's more to it than that, but that will do as
a starting point.) While I can use the XMLHTTP object via classic asp, I
would prefer to use the httpWebRequest object in asp.net. The problem is
that I don't program very well in vbscript and all the examples seem to be
in vbscript. I do program in JavaScript, c, c++, php, etc (it's a long
list) I spend a lot of time in JavaScript and have written asp pages in the
past using it as the server side scripting language.
** Tom **
 
Ad

Advertisements

Joined
Nov 27, 2014
Messages
1
Reaction score
0
I found this thread while attempting to find the answer which apparently doesn't exist in any conventional form. If found the first line in what can only be described as a urine soaked back alley of the Internet (new accounts can't post links). From there I used it's methodology to reverse engineer it's completed C#, C++ and VBScript equivalents with considerable time, trial and error. I return to you battered, bruised, late, but triumphant.

HttpWebRequest for JScript.NET

Code:
<%@ Import Namespace="System.Net"%>
<%@ Import Namespace="System.IO"%>
<%
var url = "some website URL"
var outputString = ""
var req : HttpWebRequest = HttpWebRequest(WebRequest.Create(url))
var reqResp : HttpWebResponse = HttpWebResponse(req.GetResponse())
var streamResponse : Stream = reqResp.GetResponseStream();
var streamRead : StreamReader = new StreamReader(streamResponse);
var chunkSize = 256
var readBuff = new Char[chunkSize];
var count = streamRead.Read(readBuff,0,chunkSize);
while (count > 0){
	for (var i = 0; i < count; i++){
		outputString+=readBuff[i]
	}
	count = streamRead.Read(readBuff,0,chunkSize);
}
streamResponse.Close();
streamRead.Close();
reqResp.Close()
Response.Write("Output: " + outputString)
%>

Note: Make sure to include the http part in the url string, it's picky. :)

Note: readBuff contains the read information each loop of the buffer. Each character read is placed individually into it's object so you have to iterate through it to form the result into a string or your left with a ton of commas.

For converting new lines into break tags use this loop instead:
Code:
while (count > 0){
	for(var i = 0; i < count; i++){
		var str = readBuff[i]
		if (str != "\r" && str != "\n"){
			outputString += str
		}
		else if (str == "\n"){
			outputString += "<br>"
		}
	}
	count = streamRead.Read(readBuff,0,chunkSize);
}

For analyzing head information:
Code:
for (var prop in reqResp.Headers){
	Response.Write(prop + "=" + reqResp.Headers[prop] + "<br>")
}
 
Last edited:
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top