使用 HttpWebRequest 登录页面 [英] Login to the page with HttpWebRequest

查看:32
本文介绍了使用 HttpWebRequest 登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何登录此页面http://www.bhmobile.ba/portal/index 通过使用 HttpWebRequest?

登录按钮是Pošalji";(左上角).

登录页面的HTML源代码:

<div style="margin-bottom:12px"><table class="leftbox"样式=高度:184px;背景图片:url(/web/2007/slike/okvir.jpg);"单元格间距=0"单元格填充=0"><tr><th style="vertical-align:middle"><form action="http://sso.bhmobile.ba/sso/login";方法=发布"onSubmit="return checkUserid();"><输入类型=隐藏"名称=领域"值=sso"><输入类型=隐藏"名称=应用程序"值=门户"><输入类型=隐藏"名称=网址"value=http://www.bhmobile.ba/portal/redirect?type=ssologin&url=/portal/show?idc=1111">
<tr><th style="vertical-align:middle;text-align:right;padding-right:4px;">Korisnik:</th><td><输入类型=文本"尺寸=20"id="用户名"name="userid"/></tr><tr><th style="text-align:right;padding-right:4px;">Lozinka:</th><td><输入类型=密码"尺寸=20"名称=密码"/></td></tr><tr><th colspan=2"><输入类=dugmic";类型=图像"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>
<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/>

</form></th></tr></table></div>

表单操作是http://sso.bhmobile.ba/sso/login.我如何将它与 HttpWebRequest 一起使用来获取 cookie 并使用此页面中的某个日期?

解决方案

创建一个新的 default.aspx,并将其放在后面的代码中:我无法根据您当前的问题进一步测试,因为您没有包含有效的用户名/密码.

 使用系统;使用 System.Web;使用 System.Net;使用 System.IO;使用 System.Web.UI;使用 System.Web.UI.WebControls;命名空间 Foo{公共部分类 _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 (兼容; 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 用户名 = "用户名";String PassWord = "密码";StreamWriter sw = new StreamWriter(req.GetRequestStream());sw.Write(应用=门户&安培; URL = HTTP%3A%2F%2Fwww.bhmobile.ba%2Fportal%2Fredirect%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Ftype%3Dssologin%26url%3D%2Fportal%2Fshow%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Fidc%3D1023278&安培;realm=sso&userid=" + 用户名 + "&password=" + 密码 + "&x=16&y=11");sw.关闭();HttpWebResponse 响应 = (HttpWebResponse)req.GetResponse();StreamReader reader = new StreamReader(response.GetResponseStream());字符串 tmp = reader.ReadToEnd();foreach(响应 Cookie 烹饪.Cookies){tmp += "
" + cook.Name + ":" + cook.Value;}Response.Write(tmp);Response.End();}}}

How can I login to the this page http://www.bhmobile.ba/portal/index by using HttpWebRequest?

Login button is "Pošalji" (upper left corner).

HTML source of login page:

<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>

Form action is http://sso.bhmobile.ba/sso/login. How can I use this with HttpWebRequest to get a cookie and use some date from this page?

解决方案

Make a new default.aspx, and put this in the code behind: I cant test any further based on your current question, because you didn't include a valid username/password.

    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 += "
" + cook.Name + ": " + cook.Value;
            }


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

        }
    }
}

这篇关于使用 HttpWebRequest 登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆