如何在Classic ASP中将数据发布到远程URL?

时间:2008-12-19 17:20:54

标签: asp-classic

我需要POST数据到脚本中间的网址。

  1. 用户填写表格:
  2. 表单提交到process.asp:此时我需要POST数据到第三方集成。
  3. process.asp完成并指示用户感谢您的页面。

8 个答案:

答案 0 :(得分:30)

我不确定为什么每个人都在发布ASP.Net解决方案时,你明确表示你正在使用ASP“经典”。

这样的事情应该有效。我没有写代码;我在别处找到了它。但是,如果您不想购买商业广告,则可以使用MSXML2.ServerXMLHTTP对象。

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 

您可能需要添加一些错误处理代码,以便在生产环境中使用。我相信如果遇到404或超时错误,该对象会抛出错误。你需要通过在.Send之前设置On Error Resume Next来“捕获”它们的ASP样式(yuck),然后检查ASP错误对象以查看是否存在问题。

祝你好运!

答案 1 :(得分: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
    Else
        Err.Raise 1001, "postFormData", "Post to " & url & " failed with " & xhr.Status
    End If
End Function

创建数据时,需要对数据值进行url编码。由于ASPs Server.URLEncode方法只进行路径编码而不进行组件编码,因此需要用%2F替换/字符

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

答案 2 :(得分:2)

在.Net中,它是System.Net.WebClient或System.Net.HttpWebRequest。

经典ASP有一个完全不同的api我不知道你会在那里使用什么。

[编辑]
我怀疑如果经典asp对此有任何内置支持,它就在Scripting对象中,如下所示:CreateObject("Scripting.????")

答案 3 :(得分:2)

如果你坚持使用ASP类,你可以在这里使用商业ASPHTTP库:

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

答案 4 :(得分:1)

确定所有答案都非常复杂,我知道您已经选择了一个解决方案 - 但我觉得简单的 Server.Transfer()命令可以完全满足您的需求。

在脚本末尾而不是Response.Redirect(url)到新页面 - 只需执行Server.Transfer(url),它会将整个Request集合传递到下一页。

了解它here(support.microsoft.com)。

有一些捕获(即它在浏览器上保留相同的URL,因此它可以使用后退按钮等操作),但除此之外它非常简单。

答案 5 :(得分:0)

在ASP.NET中,它非常简单:

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

答案 6 :(得分:0)

你可以通过多种方式实现。使用 WebClient

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

或者您可以将数据放入流中以在程序中使用它:

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

但我更喜欢使用HttpWebRequest

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

我更喜欢第二个,因为你可以为POST / GET或Cookie提供更多选项。

答案 7 :(得分:0)

使用此处描述的类。这是一个非常好的方法,我一直都在使用它:

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

使用它像:

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