: Sample code for making a HTTP request
Java/JSP
<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%
// setup the request
// REQUEST URL should be replaced by the URL you need to request
// For example: http://www.websitetoolbox.com/tool/register/USERNAME/create_account
URL u = new URL("REQUEST URL");
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// add the query string
// For example: String query = "username=joe&pw=secret";
String query = "";
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(query);
pw.close();
// get the input from the request
BufferedReader in = new BufferedReader(
new InputStreamReader(uc.getInputStream()));
String res = in.readLine();
in.close();
%>
Perl
(This method requires the LWP::UserAgent module. View the documentation.)
#!/usr/bin/perl
# setup the request
use LWP::UserAgent;
$ua = new LWP::UserAgent;
# REQUEST URL should be replaced by the URL you need to request
# For example: http://www.websitetoolbox.com/tool/register/USERNAME/create_account
$req = new HTTP::Request 'POST','REQUEST URL';
$req->content_type('application/x-www-form-urlencoded');
# add the query string
# For example: $query = "username=joe&pw=secret";
$query = "";
$req->content($query);
# send the request
$res = $ua->request($req);
if ($res->is_error) {
# HTTP error
}
# the $res->content variable holds the result
print "content-type: text/plain\n\n";
PHP 4.1
<?php
// setup the request
// PATH_TO_FILE should be replaced by the full path to the requested file.
// For example: /tool/register/USERNAME/create_account
$header .= "POST PATH_TO_FILE HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.websitetoolbox.com', 80, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
}
?>
JavaScript
(This method requires that the browser being used supports the XMLHttpRequest object. Most modern browsers do support it.)
Documentation:
<script>
<!--
var myxmlhttp;
doRequest();
function doRequest () {
// REQUEST URL should be replaced by the URL you need to request
// For example: http://www.websitetoolbox.com/tool/register/USERNAME/create_account
var url = "REQUEST URL";
myxmlhttp = CreateXmlHttpReq(resultHandler);
if (myxmlhttp) {
XmlHttpGET(myxmlhttp, url);
} else {
alert("An error occured while attempting to process your request.");
// provide an alternative here that does not use XMLHttpRequest
}
}
function resultHandler () {
// request is 'ready'
if (myxmlhttp.readyState == 4) {
// success
if (myxmlhttp.status == 200) {
alert("Success!");
// myxmlhttp.responseText is the content that was received from the request
} else {
alert("There was a problem retrieving the data:\n" + req.statusText);
}
}
}
function CreateXmlHttpReq(handler) {
var xmlhttp = null;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// users with activeX off
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
if (xmlhttp) xmlhttp.onreadystatechange = handler;
return xmlhttp;
}
// XMLHttp send GEt request
function XmlHttpGET(xmlhttp, url) {
try {
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
} catch (e) {}
}
-->
</script>
ASP
<%
' Start out declaring our variables.
' You are using Option Explicit aren't you?
Dim objWinHttp
Dim strHTML
' New WinHTTP v5.1 - from MS:
'
' With version 5.1, WinHTTP is now an operating-system component
' of the following systems:
' - Microsoft Windows Server 2003 family
' - Microsoft Windows XP, Service Pack 1
' - Microsoft Windows 2000, Service Pack 3 (except Datacenter Server)
Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5")
' Full Docs:
' http://msdn.microsoft.com/library/en-us/winhttp/http/portal.asp
'
' If you have trouble or are getting connection errors,
' try using the proxycfg.exe tool.
' Here we get the request ready to be sent.
' First 2 parameters indicate method and URL.
' The third is optional and indicates whether or not to
' open the request in asyncronous mode (wait for a response
' or not). The default is False = syncronous = wait.
' Syntax:
' .Open(bstrMethod, bstrUrl [, varAsync])
' REQUEST URL should be replaced by the URL you need to request
' For example: http://www.websitetoolbox.com/tool/register/USERNAME/create_account
objWinHttp.Open "GET", "REQUEST URL"
' Send it
objWinHttp.Send
' Print out the request status:
Response.Write "Status: " & objWinHttp.Status & " " _
& objWinHttp.StatusText & "<br />"
' Get the text of the response.
strHTML = objWinHttp.ResponseText
' Trash our object now that I'm finished with it.
Set objWinHttp = Nothing
%>
|