[英]How can I simulate a visit to a url?
我有一個用戶提交訂單的頁面,在他們提交訂單后,我想點擊一個網址( http://externalsite.com?id=12345&sessionid=abc123
)而不實際將它們重定向到外部網頁。
有沒有辦法做到這一點?
當然,從服務器端代碼中使用HttpWebRequest
。 這是一個例子:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
"http://externalsite.com?id=12345&sessionid=abc123");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string result = reader.ReadToEnd();
// Process the response text if you need to...
}
如果您需要http://externalsite.com/
上的用戶cookie(登錄詳細信息和其他用戶設置),您可以嵌入<iframe>
或偽造圖像或使用用戶瀏覽器的ajax請求。
使用<iframe>
:
<iframe src="http://externalsite.com?id=12345&sessionid=abc123" width="1" height="1" frameborder="0"></iframe>
使用“偽造”圖像請求(如果您可以忽略任何潛在的圖像類型問題):
<img src="http://externalsite.com?id=12345&sessionid=abc123" width="1" height="1" />
以最簡單的形式使用jQuery的跨瀏覽器ajax支持 :
$.ajax({
url: "http://externalsite.com?id=12345&sessionid=abc123"
});
您還可以應用其他格式來隱藏iframe或圖像,或者當它用於訪問其他服務器時,使用javascript將其刪除。
您可以使用WebClient類在服務器端Asp.Net代碼中發出HTTP請求。 然后,您可以使用生成的html執行任何您喜歡的操作。
結合上面的兩個答案,來自voithos和Joel Purra,我建議你考慮第三種選擇。 以下是我對每個人的評估:
1)更有把握的方法是點擊站點是將服務器端作為操作處理程序的一部分來實際提交用戶信息。 這可以通過上面的voithos方法輕松完成:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
"http://externalsite.com?id=12345&sessionid=abc123");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string result = reader.ReadToEnd();
// Process the response text if you need to...
}
這允許您的服務器訪問其服務器並確保它被調用。 這種方法的缺點是它使您的服務器具有輔助HTTP請求的開銷。 如果另一台服務器運行緩慢,則可能會產生阻塞問題,從而導致結果明顯變慢。 你可以通過多線程/異步來解決這個問題 - 但是你得到的結論是:它引入了一些無法控制的問題 - 但它確保你知道遠程源是否被擊中,以及回復是什么。
2)或者,如果您的用戶正在發布實際帖子並將HTML響應作為新頁面返回,您可以簡單地將Joel Purra的響應注入到結果頁面的HTML中,從而迫使用戶的瀏覽器負責點擊遠程服務器。
<div style="display: none;">
<iframe src="http://externalsite.com?id=12345&sessionid=abc123"></iframe>
</div>
這種方法的缺點是,如果由於某種原因,您的客戶端觸發請求並且不等待下一頁加載,外部站點將返回404錯誤等,不僅外部處理不會完成 - 你不會知道它沒有完成。
3)如果你有能力使用像jQuery這樣的客戶端庫來進行處理,我建議你讓所有的表單提交都是在線和異步進行的。 方法是這樣的:
<script type="text/javascript">
$(document).bind('ready', function () {
$('#formSubmitButton').bind('click', function (ev) {
ev.preventDefault(); // These two lines stop the default processing from
ev.stopPropagation(); // occurring on form-submit (i.e. no full post-back)
// This line starts an asynchronous call to the server, posting your form
// data itself.
$.ajax({
url: '/My/Post/Url',
type: 'POST',
async: false,
// You could use a library for this kind of form parsing. I suggest
// http://www.json.org/js.html - for serialization, and
// http://code.google.com/p/form2js/ - for form conversion. It's great.
data: { my: 'form', data: 'fields' },
success: function (data) {
$.ajax({
url: '/The/External/Url',
type: 'POST',
async: false,
data: { external: 'data', goes: 'here' },
success: function (remoteData) {
if (remoteData) // validate response here
displaySuccess();
else
displayFailure();
},
error: displayFailure
});
},
error: displayFailure
});
});
});
</script>
在此方法中 - 您將郵件發送到您自己的服務器,並且在成功時 - 立即啟動對遠程服務器的第二個請求。 但是,因為等到第二個請求被觸發之后你等待向用戶顯示成功/失敗 - 你知道至少在UI層,兩個請求都是在客戶端獲得離開頁面的隊列之前做出的。
所以 - 從工作流程和開銷的角度來看可能更安全 - 但是,它需要您在JavaScript中編寫一些UI級邏輯,這可能會有問題,具體取決於您的項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.