使用C#和HttpRequest的一个网站登录(Login on a Website using c

2019-06-27 19:54发布

我想登录使用C#与HttpRequest的一个网站上。 我已经在搜索和谷歌在这个论坛上,但我还是有些问题。 继承人我的代码:

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace BodytelConnection
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        webBrowser1.Navigate("http://www.bodytel.com/");
    }


    private void loginBtn_Click(object sender, RoutedEventArgs e)
    {
        string benutzername = textBox_benutzername.ToString();   // used just for test
        string passwort = textBox_passwort.ToString();
        string cookieHeader;
        passwort = changeString(passwort);
        benutzername = changeString(benutzername);

        HttpWebRequest req =                                                          (HttpWebRequest)HttpWebRequest.Create("http://www.bodytel.com/");
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1;rv:15.0) Gecko/20100101 Firefox/15.0)";
        req.Method = "POST";
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        req.Headers.Add("Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
        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 = "https://secure.bodytel.com/de/mybodytel.html";
        req.AllowAutoRedirect = true;
        req.ContentType = "application/x-www-form-urlencoded";

        byte[] bytes = Encoding.ASCII.GetBytes("login=hans-neo@web.de%password=xxxxxstep=login");
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        WebResponse resp = req.GetResponse();
        cookieHeader = resp.Headers["Set-cookie"];

        webBrowser1.Navigate("https://secure.bodytel.com/de/mybodytel.html");
    }

    private string changeString(string myString)
    {

        myString = myString.Replace("System.Windows.Controls.TextBox: " ,"");
        return myString;
    }


    }

}

我不能登录到网站,我只是不知道问题出在哪里。 我怎样才能登录到这个网站?

谢谢 ;)

Answer 1:

Asuming没有什么错SSL和你实际上从服务器收到一个有效的响应,你可以尝试正确编码你的价值观:

string loginstr = string.Format("login={0}&password={1}&step=login",
    HttpUtility.UrlEncode("hans-neo@web.de"),
    HttpUtility.UrlEncode("xxxxx"));
byte[] bytes = Encoding.ASCII.GetBytes(loginstr);


Answer 2:

我解决了这个问题。

下面的代码:

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;

namespace BodytelConnection{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        webBrowser1.Navigate("http://www.bodytel.com/");
    }

    private void textBox_benutzername_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void textBox_passwort_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void loginBtn_Click(object sender, RoutedEventArgs e)
    {
        string benutzername = textBox_benutzername.ToString();
        string passwort = textBox_passwort.ToString();
        string cookieHeader;
        passwort = changeString(passwort);
        benutzername = changeString(benutzername);

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://secure.bodytel.com/de/mybodytel.html");
        CookieContainer cookieContainer = new CookieContainer();
        request.CookieContainer = cookieContainer;

        request.Method = "POST";
        string postData = "login=hans-neo@web.de&password=xxxxx&step=login";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string Text = "";

        foreach (Cookie cook in response.Cookies)
        {
            Text += "COOKIE: " + cook.Name + " = " + cook.Value + "\r\n";

        }
        request.AllowAutoRedirect = false;
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();

        webBrowser1.Navigate("https://secure.bodytel.com/de/mybodytel.html");
    }

    private string changeString(string myString)
    {

        myString = myString.Replace("System.Windows.Controls.TextBox: ", "");
        return myString;
    }


}

}


Answer 3:

看起来你使用了错误的URL登录。 当我使用的登录表单中铬,看看它会发出什么样子的:

铬Networktab

因此,它在记录了“https://secure.bodytel.com/de/mybodytel.html”。 我不知道如果HttpWebRequest的可管理SSL的连接就是这样,但你需要创建WebRequest对象时使用这个链接。



文章来源: Login on a Website using c# and HTTPrequest