[ACCEPTED]-How do I POST data to a remote URL in Classic ASP?-asp-classic

Accepted answer
Score: 35

I'm not sure why everybody else is posting 14 ASP.Net solutions when you specifically 13 said you're using ASP "classic."

Something 12 like this should work. I didn't write the 11 code; I found it elsewhere. But the MSXML2.ServerXMLHTTP 10 object is what you want to use if you don't 9 want to purchase something commercial.

function getHTML (strUrl)
    Set xmlHttp = Server.Createobject("MSXML2.ServerXMLHTTP")
    xmlHttp.Open "GET", strUrl, False
    xmlHttp.setRequestHeader "User-Agent", "asp httprequest"
    xmlHttp.setRequestHeader "content-type", "application/x-www-form-urlencoded"
    getHTML = xmlHttp.responseText
    set xmlHttp = Nothing   
end function 

You 8 might need to add some error-handling code 7 to that for use in a production environment. I 6 believe that object throws errors if it 5 gets a 404 or timeout error. You'll need 4 to "trap" them ASP-style (yuck) by setting 3 On Error Resume Next before the .Send and 2 then examine the ASP error object to see 1 if there was a problem.

Good luck!

Score: 5

Most form action pages accept data as a 5 POST.

Function postFormData(url, data)
    Dim xhr : Set xhr = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    xhr.open "POST", url, false
    xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    xhr.send Data
    If xhr.Status = 200 Then
       postFormData = xhr.ResponseText
        Err.Raise 1001, "postFormData", "Post to " & url & " failed with " & xhr.Status
    End If
End Function

When creating the data url encoding 4 is needed on the data values. Since ASPs 3 Server.URLEncode method only does path encoding 2 and not component encoding you need to replace 1 out / characters with %2F

Function URLEncodeComponent(value)
    URLEncodeComponent = Server.URLEncode(value)
    URLEncodeComponent = Replace(URLEncodeComponent, "/", "%2F")
End Function
Score: 2

In .Net it's System.Net.WebClient or System.Net.HttpWebRequest.

Classic 5 ASP has a completely different api- I'm 4 not sure what you would use instead there.

I 3 suspect that if classic asp has any built 2 in support for this, it's in a Scripting 1 object, like so: CreateObject("Scripting.????")

Score: 2

If you're stuck with class ASP, you can 2 do it with the commercial ASPHTTP library 1 here:


Score: 1

In ASP.NET, it's pretty simple:

HttpWebRequest r =
r.Method = "POST";
using (Stream stream = myRequest.GetRequestStream()) {
    // Write data to stream
WebResponse resp = r.GetResponse();
// Do soemthing with the resp


Score: 1

OK all the answers were very complicated 11 and I know you already selected a solution 10 - but I feel like the simple Server.Transfer() command could 9 have done exactly what you need.

At the end 8 of your script instead of Response.Redirect(url) to 7 the new page - just do a Server.Transfer(url) and 6 it will pass your entire Request collection 5 across to the next page.

Read about it here (support.microsoft.com).

There 4 are some catches (i.e. it keeps the same 3 URL on the browser so it can play tricks 2 with the back button and such) but otherwise 1 it's pretty simple.

Score: 0

Use the class described here. This is a 2 pretty good method and I use it all the 1 time:


public class  RemotePost{
     private  System.Collections.Specialized.NameValueCollection Inputs 
     = new  System.Collections.Specialized.NameValueCollection() ;

    public string  Url  =  "" ;
    public string  Method  =  "post" ;
    public string  FormName  =  "form1" ;

    public void  Add( string  name, string value ){
        Inputs.Add(name, value ) ;

     public void  Post(){
        System.Web.HttpContext.Current.Response.Clear() ;

         System.Web.HttpContext.Current.Response.Write( "<html><head>" ) ;

         System.Web.HttpContext.Current.Response.Write( string .Format( "</head><body onload=\"document.{0}.submit()\">" ,FormName)) ;

         System.Web.HttpContext.Current.Response.Write( string .Format( "<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >" ,

        FormName,Method,Url)) ;
            for ( int  i = 0 ; i< Inputs.Keys.Count ; i++){
            System.Web.HttpContext.Current.Response.Write( string .Format( "<input name=\"{0}\" type=\"hidden\" value=\"{1}\">" ,Inputs.Keys[i],Inputs[Inputs.Keys[i]])) ;
        System.Web.HttpContext.Current.Response.Write( "</form>" ) ;
         System.Web.HttpContext.Current.Response.Write( "</body></html>" ) ;
         System.Web.HttpContext.Current.Response.End() ;

Use it like:

RemotePost myremotepost  =  new  RemotePost() ;
myremotepost.Url  =  "http://www.jigar.net/demo/HttpRequestDemoServer.aspx" ;
myremotepost.Add( "field1" , "Huckleberry" ) ;
myremotepost.Add( "field2" , "Finn" ) ;
myremotepost.Post() ; 

More Related questions