HttpWebRequest StreamWriter错误

时间:2016-12-06 15:09:27

标签: c# httpwebrequest

您好我正在尝试为一个名为SaltyBet的网站编写一个简单的机器人,您可以用假币来打赌。

登录工作正常,但是当我尝试下注时,它会在btnBet方法中停止在StreamWriter上工作。

CookieContainer cookies = new CookieContainer();
    HttpWebRequest request = null;
    HttpWebResponse response = null;
    string returnData = string.Empty;

    private void btnAuthenticate_Click(object sender, EventArgs e)
    {
        string email = tbxEmail.Text;
        string password = tbxPassword.Text;
        bool authenticated = false;

        authenticated = logIn(email, password);

        if(authenticated)
        {
            setStatus("Login Success!");
        }else
        {
            setStatus("Login Fail!");
        }



    }

    private void btnBet_Click(object sender, EventArgs e)
    {
        webStuff("http://www.saltybet.com/ajax_place_bet.php");

        StringBuilder postData = new StringBuilder();
        postData.Append("selectedplayer=player1");
        postData.Append("&wager=100"); //get amount of money and put here 

        setStatus("1");

        using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            writer.Write(postData.ToString());


        response = (HttpWebResponse)request.GetResponse();

        //Read the web page (HTML) that we retrieve after sending the request
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            returnData = reader.ReadToEnd();

        response.Close();

        setStatus("Bet placed.");
    }


    private bool logIn(string email, string password)
    {
        webStuff("http://www.saltybet.com/authenticate?signin=1");

        StringBuilder postData = new StringBuilder();
        postData.Append(string.Format("email={0}", email));
        postData.Append(string.Format("&pword={0}", password));
        postData.Append("&authenticate=signin");

        using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            writer.Write(postData.ToString());

        response = (HttpWebResponse)request.GetResponse();

        //Read the web page (HTML) that we retrieve after sending the request
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            returnData = reader.ReadToEnd();


        response.Close();

        return !returnData.Contains("Invalid Email or Password");

    }

    private void setStatus(string text)
    {
        lblStatus.Text = "Status: " + text;
    }

    private void webStuff(string link)
    {
        //Need to retrieve cookies first
        request = (HttpWebRequest)WebRequest.Create(new Uri(link));
        request.Method = "GET";
        request.CookieContainer = cookies;
        response = (HttpWebResponse)request.GetResponse();

        //Set up the request
        request = (HttpWebRequest)WebRequest.Create(new Uri(link));
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
        request.Referer = link;
        request.AllowAutoRedirect = true;
        request.KeepAlive = true;
        request.CookieContainer = cookies;
    }
}

}

0 个答案:

没有答案