Sending data using HttpWebRequest using the login page

I am trying to send data for this page using the HttpWebRequest class:

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

but I ran into login authentication problem. heres my code:

  System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); string postData = "ctlMessageID=" + 348; postData += ("&ctlUserID=" + 7); postData += ("&ctlTitle=" + 7); postData += ("&ctlEmail=" + " rrawhi@gmail.com "); 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.

+6
authentication c # post
source share
4 answers

First of all, it looks like you are not actually sending the request. To send a POST request to the server, you need to request a 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're submitting is looking for an established and terminated session. First try sending your credentials to the login page (http://stage1.darotools.com/Quran.v1.admin/Login.asp). Install HttpWebRequest.CookieContainer in a new instance of CookieContainer (). Then make another message on the CreateForm.asp page, but be sure to set the new CookieContainer instance for the new HttpWebRequest.CookieContainer object that you used when you made the POST on the login page. Then the cookies received from the login page will be sent to the CreateForm.asp page and the session will be “supported” from the server’s point of view. For example:

 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(); } 
+4
source

Try using:

 myRequest.Credentials = new NetworkCredential("username", "password", "domain"); // domain is not needed in case of forms authentication 

If this does not work, you can authenticate the user on the login page and pass the CookieContainer, and then reuse the CookieContainer when requesting the necessary page.

+1
source

Perhaps this link will help you:

webrequest login session

save session id over httpwebrequest

0
source

There are several different things that may be here.

Try setting some credentials.

 myRequest.Credentials = CredentialCache.DefaultCredentials; // if we have a proxy set its creds as well if( myRequest.Proxy != null ) { myRequest.Proxy.Credentials = CredentialCache.DefaultCredentials; } 

And make sure that you also set UserAgent and Accpet.

 myRequest.UserAgent = "Foo"; myRequest.Accept = "*/*"; 

If you add them, I do not think that you will have problems.

0
source

All Articles