How to identify login successful using Httpwebrequest and httpwebresponse?

2.5k views Asked by At

I doesn't understand how do I check login is successful or not. How can I check login success using httpwebrequest and httpresponse?

my Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace Way2sms
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private CookieCollection cc = new CookieCollection();
    private void Send_Click(object sender, EventArgs e)
    {
        string uid = txtUsername.Text.Trim();
        string password = txtpass.Text.Trim();
        string message = txtmsg.Text.Trim();
        string no = txtno.Text.Trim();

        try
        {
            WebBrowser wb = new WebBrowser();

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://site5.way2sms.com/Login1.action");

            req.Method = "POST";
            CookieContainer con = new CookieContainer();
            req.CookieContainer = con;
            req.CookieContainer.Add(GetC());
            req.KeepAlive = true;

            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";

            req.ContentType = "application/x-www-form-urlencoded";
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

            req.Referer = "http://site5.way2sms.com/content/index.html";

            byte[] data = System.Text.Encoding.Default.GetBytes("username=" + uid + "&password=" + password);

            req.ContentLength = data.Length;

            req.AllowAutoRedirect = true;

            req.ServicePoint.Expect100Continue = true;

            Stream str = req.GetRequestStream();

            str.Write(data, 0, data.Length);

            str.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            wb.ScriptErrorsSuppressed = true;
            wb.DocumentText = new StreamReader(res.GetResponseStream()).ReadToEnd();

            webBrowser1.Navigate(wb.DocumentText);


            HttpWebRequest X = (HttpWebRequest)WebRequest.Create("http://site5.way2sms.com/quicksms.action");
            X.Method = "POST";
            X.CookieContainer = con;
            X.KeepAlive = true;
            X.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";

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

            X.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

            X.Referer = "http://site5.way2sms.com/jsp/InstantSMS.jsp";

            byte[] datax = Encoding.Default.GetBytes("HiddenAction=instantsms&catnamedis=Birthday&Action=sa65sdf656fdfd&chkall=on&MobNo=" + no + "&textArea=" + message);

            X.ContentLength = datax.Length;

            X.AllowAutoRedirect = true;

            X.ServicePoint.Expect100Continue = true;

            str = X.GetRequestStream();

            str.Write(datax, 0, datax.Length);

            str.Close();

            HttpWebResponse resx = (HttpWebResponse)X.GetResponse();

            wb.DocumentText = new StreamReader(resx.GetResponseStream()).ReadToEnd();
        }
        catch (Exception ex)
        { 

        }

    }
    private CookieCollection GetC()
    {

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://site5.way2sms.com/");

        req.CookieContainer = new CookieContainer();

        req.CookieContainer.Add(cc);

        req.CookieContainer.Add(cc);

        req.KeepAlive = true;

        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";

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

        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

        req.Referer = "http://site5.way2sms.com/content/index.html";

        req.AllowAutoRedirect = true;

        req.ServicePoint.Expect100Continue = true;

        return ((HttpWebResponse)req.GetResponse()).Cookies;
    }

Here is my code for sending sms using way2sms. I want to check enter user name and password is correct or not? so How can I check?

1

There are 1 answers

1
Steven On

Edited

In order to do what you need, you will need to understand HttpWebRequest and HttpWebResponse. Look at this article which will give you a good picture.

Using HttpWebRequest and HttpWebResponse

After that, you can search through your response by this function:

WebRequest _request = (HttpWebRequest)WebRequest.Create("UrlToGet");
using (WebResponse response = _request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        string text = reader.ReadToEnd();
    }
}

What you do with the "text" variable, is upto you.