我需要POST
數據到腳本中間的url。如何將數據傳遞到經典ASP中的遠程URL?
- 用戶填寫表單:
- 表單提交到
process.asp
:我需要POST
數據的第三方集成在這一點上。 process.asp
完成並引導用戶感謝您的頁面。
我需要POST
數據到腳本中間的url。如何將數據傳遞到經典ASP中的遠程URL?
process.asp
:我需要POST
數據的第三方集成在這一點上。process.asp
完成並引導用戶感謝您的頁面。我不知道爲什麼別人都張貼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錯誤對象以查看是否有問題。
祝你好運!
在.Net它是System.Net.WebClient或System.Net.HttpWebRequest。
經典的ASP有一個完全不同的API - 我不知道你會用什麼來代替。
[編輯]
我懷疑如果經典ASP有任何內置該支持,這是一個腳本對象,就像這樣:CreateObject("Scripting.????")
如果你堅持用一流的ASP,你可以
在ASP.NET中,這是很簡單的:
與商業ASPHTTP庫在這裏做即使這是過度的 - system.net.webclient將處理99%的請求在更少的代碼。 – 2008-12-19 17:39:45
你可以通過很多方式做到這一點。隨着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。
使用此處描述的類。這是一個非常好的方法,我用它所有的時間:
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() ;
大多數表單操作頁面接受數據作爲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
確定所有問題的答案是非常複雜的,我知道你已經選擇了一個解決方案 - 但我覺得像簡單的Server.Transfer()命令可以完成你所需要的。
在您的腳本結束而不是Response.Redirect(url)到新頁面 - 只需執行Server.Transfer(url),它會將您的整個請求集合傳遞到下一頁。
閱讀關於它here(support.microsoft.com)。
有一些捕獲(即它在瀏覽器上保留相同的URL,所以它可以玩後退按鈕等技巧),但否則它很簡單。
.Net也允許這種處理。只需將您的processing.asp代碼放入Click事件中的按鈕或超鏈接控件,或者如果IsPostBack和_all_表單提交應該引起此操作,則可以從Load事件中調用它。 – 2008-12-19 17:42:55
這被標記爲.NET的第20分鐘...... – 2008-12-19 17:54:22