使用HttpWebRequest登录页面

13

如何使用HttpWebRequest登录到http://www.bhmobile.ba/portal/index这个页面?

登录按钮是“Pošalji”(左上角)。

登录页面的HTML源代码:

<table id="maintable" border="0" cellspacing="0" cellpadding="0" style="height:100%; width:100%">
  <tr>
    <td width="367" style="vertical-align:top;padding:3px"><script type="text/javascript">
function checkUserid(){
    if (document && document.getElementById){
        var f = document.getElementById('userid');
        if (f){
            if (f.value.length < 8){
                alert('Korisničko ime treba biti u formatu 061/062 xxxxxx !');
                return false;
            }
        }
    }
    return true;
}
</script>
<div style="margin-bottom:12px"><table class="leftbox" style="height:184px; background-image:url(/web/2007/slike/okvir.jpg);" cellspacing="0" cellpadding="0">
    <tr>
        <th style="vertical-align:middle"><form action="http://sso.bhmobile.ba/sso/login" method="post" onSubmit="return checkUserid();">

            <input type="hidden" name="realm" value="sso">
            <input type="hidden" name="application" value="portal">
            <input type="hidden" name="url" value="http://www.bhmobile.ba/portal/redirect?type=ssologin&amp;url=/portal/show?idc=1111">
            <table class="formbox" align="center" cellspacing="0" cellpadding="0">
                <tr>
                    <th style="vertical-align:middle; text-align:right;padding-right:4px;">Korisnik:</th>
                    <td><input type="text" size="20" id="userid" name="userid"/></td>
                </tr>

                <tr>
                    <th style="text-align:right;padding-right:4px;">Lozinka:</th>
                    <td><input type="password" size="20" name="password" /></td>
                </tr>
                <tr>
                    <th colspan="2">
                         <input class="dugmic" type="image" id="prijava1" alt="Prijava" src="/web/2007/dugmici/posalji_1.jpg" onmouseover="ChangeImage('prijava1','/web/2007/dugmici/posalji_2.jpg')" onmouseout="ChangeImage('prijava1','/web/2007/dugmici/posalji_1.jpg')">
                    </th>

                </tr>
            </table>
            <div style="padding:12px;">
                <a href="/portal/show?idc=1121">Da li ste novi BH Mobile korisnik?</a><br />
                <a href="/portal/show?idc=1121">Da li ste zaboravili lozinku(šifru)?</a><br />
            </div>
        </form></th>
    </tr>

</table></div>

表单的action是http://sso.bhmobile.ba/sso/login。 如何使用HttpWebRequest获取cookie并使用来自此页面的某些数据?

2个回答

25

新建一个default.aspx文件,并在其代码后面添加以下内容:我无法进一步测试您当前的问题,因为您没有包含有效的用户名/密码。

    using System; 
    using System.Web;
    using System.Net; 
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.WebControls; 

namespace Foo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://sso.bhmobile.ba/sso/login"); 
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"; 
            req.Method = "POST";
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            req.Headers.Add("Accept-Language: en-us,en;q=0.5");
            req.Headers.Add("Accept-Encoding: gzip,deflate");
            req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            req.KeepAlive = true;
            req.Headers.Add("Keep-Alive: 300");
            req.Referer ="http://sso.bhmobile.ba/sso/login";

            req.ContentType = "application/x-www-form-urlencoded"; 

            String Username = "username";
            String PassWord = "Password";

            StreamWriter sw = new StreamWriter(req.GetRequestStream());
            sw.Write("application=portal&url=http%3A%2F%2Fwww.bhmobile.ba%2Fportal%2Fredirect%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Ftype%3Dssologin%26url%3D%2Fportal%2Fshow%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Fidc%3D1023278&realm=sso&userid=" + Username + "&password=" + password + "&x=16&y=11");
            sw.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();


            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();

            foreach (Cookie cook in response.Cookies)
            {
                tmp += "\n" + cook.Name + ": " + cook.Value;
            }


            Response.Write(tmp);
            Response.End();

        }
    }
}

在获取了StreamWriter之后,您是如何构建这个查询字符串的... sw.Write("application=portal&url=http%3A%2F%2Fww.... 更具体地说,在该字符串中如何获取jsessionid?一旦您在http://sso.bhmobile.ba/sso/login上执行了URL,需要进行哪些更改才能调用索引页面? - user20358
还有,为什么在这里使用StreamWriter sw对象?看起来并不需要它来获取登录页面后面的URL。为什么在请求中没有发送用户名/密码,例如在此行中...HttpWebResponse response = (HttpWebResponse)req.GetResponse(); - user20358
你在 sw.write("") 后面添加的那个字符串,你是怎么得到那个信息的? - deltu100
这些是表单值,它们将被发布到服务器(当然是查询字符串编码的)。 - missaghi

3
您可以使用内置的WebClient对象而不是自己创建请求。
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username", "password");
string url = "http://foo.com";          
try
{
    using (Stream stream = wc.OpenRead(new Uri(url)))
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
             }
    }
}
catch (WebException e)
{
    //Error handeling
}

1
这会存储cookies吗?我认为你需要添加一个CookieContainer对象。 - missaghi
4
这对基于表单的身份验证无效,但适用于NTLM或基于Windows的身份验证。 - user20358

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接