Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending data using HttpWebRequest with a login page

I'm trying to send data for this page by using HttpWebRequest class :

www.stage1.darotools.com/Quran.v1.admin/_FormsWizard/CreateForm.asp

but I faced a problem with the login authentication . heres my code :

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    string postData = "ctlMessageID=" + 348;
    postData += ("&ctlUserID=" + 7);
    postData += ("&ctlTitle=" + 7);
    postData += ("&ctlEmail=" + "[email protected]");
    postData += ("&ctlIsSystem=" + 0);
    postData += ("&ctlFormBody=");
    postData += ("&ctlEnableCaptcha=");
    postData += ("&ctlEmailAttachedFiles=");
    postData += ("&ctlMailingList=");
    postData += ("&ctlCommentaryTitle=" + 1);
    postData += ("&ctlIsActive=" + 2);
    postData += ("&ctlCommentaryPersonID=" + 6);
    postData += ("&ctlOrderKey=");
    postData += ("&Commentary_TextControl_html=" + "aaaaaaaaaaaa");
    postData += ("&controlValue4=" + 666666);
    postData += ("&ctlLanguageID=" + 1);
    postData += ("&ctlAya=" + 349);
    postData += ("&PathInfo=" + "dbsFramed, dbsFramed");
    postData += ("&Caller=" + "rawhi");
    byte[] data = encoding.GetBytes(postData);

    // Prepare web request...
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://stage1.darotools.com/Quran.v1.admin/_FormsWizard/CreateForm.asp");
    myRequest.Method = "POST";
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.ContentLength = data.Length;
    Stream newStream = myRequest.GetRequestStream();
    // Send the data.
    newStream.Write(data, 0, data.Length);
    newStream.Close();

And this is the Login page :

www.stage1.darotools.com/Quran.v1.admin/Login.asp

Thanks in advance.

like image 897
Rawhi Avatar asked Feb 02 '11 14:02

Rawhi


1 Answers

First off, it looks like you aren't actually sending the request. To send the POST request to the server you need to request the response:

HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
string responseContent = null;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
 {
    //get the text content of the response, if needed
    responseContent = reader.ReadToEnd();
 }

Also, it looks like the page you are posting to is looking for an established and authenticated Session. Try posting credentials to the login page (http://stage1.darotools.com/Quran.v1.admin/Login.asp) first. Set HttpWebRequest.CookieContainer to a new CookieContainer() instance. Then, do another post to the CreateForm.asp page but be sure to set the new HttpWebRequest.CookieContainer object to use the same instance of the CookieContainer you used when you did a POST to the login page. Then the cookies received from the login page will be sent to the CreateForm.asp page and the session will be "maintained" from the server's perspective. For instance:

CookieContainer m_cookies = new CookieContainer();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://stage1.darotools.com/Quran.v1.admin/Login.asp");
...

HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
HttpWebRequest formRequest = (HttpWebRequest)WebRequest.Create("http://stage1.darotools.com/Quran.v1.admin/_FormsWizard/CreateForm.asp");
formRequest.CookieContainer = myRequest.CookieContainer;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
 {
    //get the text content of the response, if needed
    responseContent = reader.ReadToEnd();
 }
like image 72
Brady Holt Avatar answered Sep 28 '22 13:09

Brady Holt