2011-11-05 80 views
0

我需要從Web應用程序獲取數據。我沒有訪問數據庫或應用程序的源(.net)。提交一個頁面,檢查正在提交的域名,檢查響應

Web應用程序就像這樣 - 在字段中輸入值,單擊提交按鈕,與這些字段關聯的數據將返回到模式彈出窗口中。

我需要做相同的編程,而實際上沒有打開瀏覽器。

我需要知道需要發佈的字段的名稱和URL。然後存儲響應。

任何.Net語言都可以。

任何線索怎麼辦?謝謝。

回答

0

我使用這些功能來發布頁面昂GET結果:

public static string HttpPost(string url, object[] postData, string saveTo = "") 
{ 
    StringBuilder post = new StringBuilder(); 
    for (int i = 0; i < postData.Length; i += 2) 
     post.Append(string.Format("{0}{1}={2}", i == 0 ? "" : "&", postData[i], postData[i + 1])); 
    return HttpPost(url, post.ToString(), saveTo); 
} 
public static string HttpPost(string url, string postData, string saveTo = "") 
{ 
    postData = postData.Replace("\r\n", ""); 
    try 
    { 
     WebRequest req = WebRequest.Create(url); 
     byte[] send = Encoding.Default.GetBytes(postData); 
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     //req.ContentType = "text/xml;charset=\"utf-8\""; 
     req.ContentLength = send.Length; 

     Stream sout = req.GetRequestStream(); 
     sout.Write(send, 0, send.Length); 
     sout.Flush(); 
     sout.Close(); 

     WebResponse res = req.GetResponse(); 
     StreamReader sr = new StreamReader(res.GetResponseStream()); 
     string returnvalue = sr.ReadToEnd(); 
     if (!string.IsNullOrEmpty(saveTo)) 
      File.WriteAllText(saveTo, returnvalue); 

     //Debug.WriteLine("{0}\n{1}", postData, returnvalue); 
     return returnvalue; 
    } 
    catch (Exception ex) 
    { 
     Debug.WriteLine("POST Error on {0}\n {1}", url, ex.Message); 
     return ""; 
    } 
} 
+0

感謝@Macro了非常詳細的答覆!它在很大程度上很好地工作。不過,我仍然有一個問題。在網頁上,它是一個AJAX回發,結果顯示在模式彈出窗口中。現在使用你的代碼,我得到了整個頁面的響應,但模式彈出窗口是錯誤的。我正在爲你的函數提供一個字符串數組,包括textboxname,value,dropdownlistname,value等等。我做錯了什麼? :-( – Upendra

+0

@Supars:你沒有做錯什麼,我提供的功能應該像你一樣使用。可能是頁面做了一些奇怪的事情,我不知道,對不起: – Marco