使用带有登录页面的HttpWebRequest发送数据

我正在尝试使用HttpWebRequest类为此页面发送数据:

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

但是我遇到了登录validation的问题。 inheritance我的代码:

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

这是登录页面:

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

提前致谢。

首先,看起来你实际上并没有发送请求。 要将POST请求发送到服务器,您需要请求响应:

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

此外,您发布的页面看起来正在寻找已建立且经过身份validation的会话。 首先尝试将凭据发布到登录页面(http://stage1.darotools.com/Quran.v1.admin/Login.asp)。 将HttpWebRequest.CookieContainer设置为新的CookieContainer()实例。 然后,再向CreateForm.asp页面发post,但一定要设置新的HttpWebRequest.CookieContainer对象,以使用你在登录页面进行POST时使用的CookieContainer的相同实例。 然后,从登录页面收到的cookie将被发送到CreateForm.asp页面,并且会从服务器的角度“维护”会话。 例如:

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

尝试使用:

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

如果这不起作用,您可以在登录页面上validation用户并在那里传递CookieContainer,然后在请求所需页面时重用该CookieContainer。

也许这个链接可以帮助你:

webrequest登录会话

将会话ID保持在httpwebrequest上

这里可能会发生一些不同的事情

尝试设置一些凭据

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

并确保您也设置了UserAgent和Accpet设置。

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

如果你添加这些我不认为你会有任何问题。