我有正常的用户名和密码可以登陆一个数据查询的介面,现在想要把查询出来的数据通过自己的程序来控制,把它写到我在本地的一个数据库中。目前只有一个大概的想法,用WebBrowser做,只是不知道应该怎样控制我所收到的页面,那个网站是用JSP做的。

解决方案 »

  1.   

    提供思路:
    用 HttpRequest 模拟Form提交。
    然后解析返回的 HttpResponse,把数据抽出来插入DB就可以乐
      

  2.   

    如何使用HttpRequest 我在C#中找不到啊
      

  3.   

    http://msdn.microsoft.com/zh-cn/library/system.web.httprequest(VS.80).aspx
      

  4.   


    using System;
    using System.Web; 
    using System.Web.SessionState; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.HtmlControls; 
    using System.Net;
    using System.Text;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Collections;namespace Utility
    {
    public class HttpRequestUtility
    {
    public HttpRequestUtility()
    {
    } public static string BuildPostDatas(Hashtable hsData)
    {
    string strPostDatas = "";
    IDictionaryEnumerator enumer = hsData.GetEnumerator();
    while(enumer.MoveNext())
    {
    strPostDatas += string.Format("{0}={1}&", enumer.Key, enumer.Value);
    }
    strPostDatas.TrimEnd('&');
    return strPostDatas;
    } /// <summary>
    /// RequestUrl: request some url, return the response.
    /// </summary>
    /// <param name="strUrl">the url</param>
    /// <param name="strPostDatas">the post data</param>
    /// <param name="objCookieContainer">cookie's(session's) container</param>
    /// <returns></returns>
    public static string RequestUrl(string strUrl, string strPostDatas, ref CookieContainer objCookieContainer)  

    HttpWebResponse res = null; 
    string strResponse = ""; try

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl); 
    req.Method = "POST"; 
    req.KeepAlive = true; 
    req.ContentType = "application/x-www-form-urlencoded";
    //req.Timeout = 20; if(objCookieContainer == null)
    objCookieContainer = new CookieContainer();
    req.CookieContainer = objCookieContainer;

    StringBuilder objEncodedPostDatas = new StringBuilder(); 
    byte[] postDatas = null; req.ContentLength = 0;
    if (strPostDatas != null && strPostDatas.Length > 0)
    {
    string[] datas = strPostDatas.TrimStart('?').Split(new char[] {'&'});
    for(int i=0; i<datas.Length; i++)
    {
    string[] keyValue = datas[i].Split(new char[] {'='});
    if(keyValue.Length >= 2) 
    {
    objEncodedPostDatas.Append(HttpUtility.UrlEncode(keyValue[0]));
    objEncodedPostDatas.Append("=");
    objEncodedPostDatas.Append(HttpUtility.UrlEncode(keyValue[1]));
    if(i < datas.Length - 1) 
    {
    objEncodedPostDatas.Append("&");
    }
    }
    }
    postDatas = Encoding.UTF8.GetBytes(objEncodedPostDatas.ToString()); 
    req.ContentLength = postDatas.Length;
    using(Stream reqStream = req.GetRequestStream())
    {
    reqStream.Write(postDatas, 0, postDatas.Length); 
    }
    } res = (HttpWebResponse)req.GetResponse(); 
    objCookieContainer = req.CookieContainer;
    using(Stream resStream = res.GetResponseStream())
    {
    using(StreamReader sr = new StreamReader(resStream, Encoding.UTF8))
    {
    strResponse = sr.ReadToEnd();
    }
    }
    }
                //catch (Exception ex)
                //{
                //    strResponse = ex.ToString();
                //}  
    finally

    if (res != null)  

    res.Close();

    }
    return strResponse;
    }        public static string RequestUrl(string strUrl, ref CookieContainer objCookieContainer)
            {
                HttpWebResponse res = null;
                string strResponse = "";            try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
                    req.Method = "GET";                if (objCookieContainer == null)
                        objCookieContainer = new CookieContainer();                req.CookieContainer = objCookieContainer;
                    res = (HttpWebResponse)req.GetResponse();
                    objCookieContainer = req.CookieContainer;                using (Stream resStream = res.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(resStream, Encoding.UTF8))
                        {
                            strResponse = sr.ReadToEnd();
                        }
                    }
                }
                //catch (Exception ex)
                //{
                //    strResponse = ex.ToString(); 
                //}
                finally
                {
                    if (res != null)
                    {
                        res.Close();
                    } 
                }            return strResponse;
            }
    }
    }
      

  5.   

    C#中HttpRequest可以向网站提交内容吗
      

  6.   

    不好意思订正一下: 用 HttpWebRequest , 不是 HttpRequest(HttpRequest就是ASP.NET里的Request对象)
    用HttpWebRequest可以提交数据。我贴的代码一个用POST提交,一个用GET(就是通过URL传值)提交。例:string strPostDatas = string.Format("username={0}&password={1}&lawEmp=on", strUserName, strPasswd);
    CookieContainer cc = new CookieContainer();
    strResposne = HttpRequestUtility.RequestUrl("http://www.xxx.com/xxx.jsp", strPostDatas, ref cc);