Post Data To ASP.NET From WINFORM

truedeity

Newcomer
Joined
Jun 12, 2010
Messages
1
I've been tryna do this for a long time now.

this is the url i want to post at.

http://promalechoice.com/post/pro-male-choice-is-not-bad.aspx

///Create post information.

StringBuilder baseRequest = new StringBuilder();

baseRequest.Append("txtName634119556352311642");
baseRequest.Append("=");
baseRequest.Append(System.Web.HttpUtility.UrlEncode("hulk hogan"));
baseRequest.Append("&");


baseRequest.Append("ctl00$cphBody$CommentView1$txtEmail");
baseRequest.Append("=");
baseRequest.Append(System.Web.HttpUtility.UrlEncode("hulkmedia@gmail.com"));
baseRequest.Append("&");

baseRequest.Append("ctl00$cphBody$CommentView1$txtWebsite");
baseRequest.Append("=");
baseRequest.Append(System.Web.HttpUtility.UrlEncode("www.hulkmedia.net"));
baseRequest.Append("&");

baseRequest.Append("ctl00$cphBody$CommentView1$txtContent");
baseRequest.Append("=");
baseRequest.Append(System.Web.HttpUtility.UrlEncode("I really have nothing more to say than I love posting via a winforms application..."));





//Create request object.


Uri uri = new Uri(@"http://promalechoice.com/post/pro-male-choice-is-not-bad.aspx");


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);


request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)";

string result = "";


request.CookieContainer = new CookieContainer();
string viewstate;
request.ContentLength = 0;



using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();

//now extract the viewstate from the url. according to what ive read if you want to post to a asp.net form you need to include the viewstate in the postdata.
viewstate = Regex.Match(result, "(?<=__VIEWSTATE\" value=\")(?<val>.*?)(?=\")").Groups["val"].Value.ToString();

baseRequest.Append("&");
baseRequest.Append("__VIEWSTATE");
baseRequest.Append("=");
baseRequest.Append(viewstate); //Should the viewstate have a urldecode, encode, or none?


using (Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(baseRequest.ToString());
writeStream.Write(bytes, 0, bytes.Length);
writeStream.Flush();


}




}
}
}



right now its saying that i cant open the request more than once. however, i need to be able to get the __VIEWSTATE into the postdata.

perhaps i can set the timeout on the request object to 0?

someone guide me in the right direction i just wana create a post on an asp.net webfrom from a winform thats it. hopefully someone can create a working example using the url I provided...

thanks for anything you can provide.
 
Back
Top