一个登录页面发送HttpWebRequest的使用数据 [英] sending data using HttpWebRequest with a login page

查看:125
本文介绍了一个登录页面发送HttpWebRequest的使用数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用HttpWebRequest类发送本页数据:

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

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

但我面临的登录验证的问题。
我的继承人code:

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=" + "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

先谢谢了。

推荐答案

首先,它看起来像你实际上并没有发送请求。要发送POST请求,则需要请求的响应服务器:

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

此外,它看起来像您要发布帖子正在寻找建立和验证会话页面。尝试张贴凭据登录页面(http://stage1.darotools.com/Quran.v1.admin/Login.asp)第一。设置HttpWebRequest.CookieContainer到一个新的CookieContainer()的实例。然后,在另一个帖子的CreateForm.asp页面,但一定要设置新HttpWebRequest.CookieContainer对象使用,当你做一个POST到登录页面,您使用的CookieContainer的同一个实例。然后从登录页面收到的cookies将被发送到CreateForm.asp页面,该会话将被保留,从服务器的角度来看。例如:

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

这篇关于一个登录页面发送HttpWebRequest的使用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆