【问题标题】:How do I POST data to a remote URL in Classic ASP?如何在 Classic ASP 中将数据发布到远程 URL?
【发布时间】:2017-08-24 13:59:17
【问题描述】:

我需要POST数据到脚本中间的一个url。

  1. 用户填写表单:
  2. 表单提交至process.asp:此时我需要将POST 数据提交给第三方集成。
  3. process.asp 完成并引导用户访问感谢页面。

【问题讨论】:

  • .Net 也允许这种处理。只需将您的 processing.asp 代码放在按钮或超链接控件的 Click 事件中,或者如果 IsPostBack 和 所有 表单提交应该导致这种情况,甚至可以从 Load 事件中调用它。
  • 这在前 20 分钟被标记为 .NET...

标签: asp-classic


【解决方案1】:

当您明确表示您使用的是“经典”ASP 时,我不确定为什么其他人都发布 ASP.Net 解决方案。

这样的事情应该可以工作。我没有写代码;我在别处找到的。但是,如果您不想购买商业产品,则可以使用 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 样式(糟糕),然后检查 ASP 错误对象以查看是否存在问题。

祝你好运!

【讨论】:

  • 这个问题最初有几个 .Net 引用,后来被删掉了。
  • Content-Type 对于 GET 请求是多余的,没有内容。 User-Agent 很有趣,但也是多余的。
  • UserAgent 并不总是多余的——您实际上可能会在响应中得到不同的内容,具体取决于它。
【解决方案2】:

大多数表单操作页面接受数据作为 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 编码。由于 ASP 的 Server.URLEncode 方法只进行路径编码而不是组件编码,因此您需要将 / 替换为 %2F

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

【讨论】:

    【解决方案3】:

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

    Classic ASP 有一个完全不同的 api——我不确定你会在那里使用什么。

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

    【讨论】:

      【解决方案4】:

      如果你被 ASP 类卡住了,你可以在这里使用商业 ASPHTTP 库:

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

      【讨论】:

        【解决方案5】:

        在 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
        

        【讨论】:

        • 即使这样做有点过头了——system.net.webclient 将用更少的代码处理 99% 的请求。
        【解决方案6】:

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

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

        阅读here (support.microsoft.com)。

        有一些问题(即它在浏览器上保持相同的 URL,因此它可以使用后退按钮等来玩花样)但除此之外它非常简单。

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

            您可以通过多种方式做到这一点。与 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。

            【讨论】:

              猜你喜欢
              • 2016-09-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-20
              • 1970-01-01
              • 2016-04-28
              • 1970-01-01
              相关资源
              最近更新 更多