使用c#和HTTPrequest登录网站

我想使用c#登录带有HTTPrequest的网站.我已经在google和这个论坛上搜索了但我还是有些问题.继承我的代码:

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


    }

}

我无法登录该网站,我只是不知道问题出在哪里.我该如何登录这个网站?

谢谢 😉

最佳答案
假设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);

转载注明原文:使用c#和HTTPrequest登录网站 - 代码日志