23

I need to POST data to a url in the middle of a script.

  1. User fills out form:
  2. Form submits to process.asp: I need to POST data to a 3rd Party integration at this point.
  3. process.asp finishes and directs user to thank you page.
George Stocker
  • 57,289
  • 29
  • 176
  • 237
nrhammond
  • 233
  • 1
  • 2
  • 4
  • .Net allows that kind of processing, too. Just put your processing.asp code in the Click event for a button or hyperlink control, or even call it from the Load event if IsPostBack and _all_ form submissions should cause this. – Joel Coehoorn Dec 19 '08 at 17:42
  • This was tagged as .NET the first 20 minutes... – Patrick Desjardins Dec 19 '08 at 17:54

8 Answers8

35

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

Something like this should work. I didn't write the code; I found it elsewhere. But the MSXML2.ServerXMLHTTP object is what you want to use if you don't 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"
    xmlHttp.Send
    getHTML = xmlHttp.responseText
    xmlHttp.abort()
    set xmlHttp = Nothing   
end function 

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

Good luck!

John Rose
  • 1,943
  • 2
  • 18
  • 29
5

Most form action pages accept data as a 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
    Else
        Err.Raise 1001, "postFormData", "Post to " & url & " failed with " & xhr.Status
    End If
End Function

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

Function URLEncodeComponent(value)
    URLEncodeComponent = Server.URLEncode(value)
    URLEncodeComponent = Replace(URLEncodeComponent, "/", "%2F")
End Function
MarkyMarksFunkyBunch
  • 1,060
  • 10
  • 10
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
2

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

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

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

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

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

http://www.serverobjects.com/comp/asphttp3.htm

Keith Palmer Jr.
  • 27,666
  • 16
  • 68
  • 105
1

In ASP.NET, it's pretty simple:

HttpWebRequest r =
  (HttpWebRequest)WebRequest.Create("http://www.google.com");
r.Method = "POST";
using (Stream stream = myRequest.GetRequestStream()) {
    // Write data to stream
}
WebResponse resp = r.GetResponse();
// Do soemthing with the resp
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
1

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

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

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

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

MikeMurko
  • 2,214
  • 1
  • 27
  • 56
0

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

http://www.jigar.net/articles/viewhtmlcontent78.aspx

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() ; 
BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
-1

You can do it by many way. With WebClient

 WebClient Client = new WebClient ();
 Client.DownloadFile("http://www.stackoverflow.com/myfile.html", "myfile.html");

Or you can put the data in a stream to use it in your program:

WebClient Client = new WebClient ();
Stream strm = Client.OpenRead ("http://www.stackoverflow.com/myfile.htm");

But I do prefer use HttpWebRequest:

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.stackoverflow.com/myfile.html");
StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream());

I prefer the second one because you can have more option for POST/GET or for Cookie.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341