2011-11-05 83 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