2008-12-19 35 views
17

我需要POST数据到脚本中间的url。如何将数据传递到经典ASP中的远程URL?

  1. 用户填写表单:
  2. 表单提交到process.asp:我需要POST数据的第三方集成在这一点上。
  3. process.asp完成并引导用户感谢您的页面。
+0

.Net也允许这种处理。只需将您的processing.asp代码放入Click事件中的按钮或超链接控件,或者如果IsPostBack和_all_表单提交应该引起此操作,则可以从Load事件中调用它。 – 2008-12-19 17:42:55

+0

这被标记为.NET的第20分钟...... – 2008-12-19 17:54:22

回答

26

我不知道为什么别人都张贴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错误对象以查看是否有问题。

祝你好运!

2

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

经典的ASP有一个完全不同的API - 我不知道你会用什么来代替。

[编辑]
我怀疑如果经典ASP有任何内置该支持,这是一个脚本对象,就像这样:CreateObject("Scripting.????")

0

在ASP.NET中,这是很简单的:

与商业ASPHTTP库在这里做
+0

即使这是过度的 - system.net.webclient将处理99%的请求在更少的代码。 – 2008-12-19 17:39:45

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。

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

大多数表单操作页面接受数据作为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时,需要对数据值进行编码。由于平均价格Server.URLEncode方法只做路径编码,而不是成份编码需要用%2F替换出/字符

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

确定所有问题的答案是非常复杂的,我知道你已经选择了一个解决方案 - 但我觉得像简单的Server.Transfer()命令可以完成你所需要的。

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

阅读关于它here(support.microsoft.com)。

有一些捕获(即它在浏览器上保留相同的URL,所以它可以玩后退按钮等技巧),但否则它很简单。

相关问题