How can I POST form data from a C#/ASP.NET to a remote website?

How can I POST form data from a C#/ASP.NET to a remote website?

I'm trying to send data to a remote PHP website. I want to send the data in sessions using HTTP POST. The thing is, my website is built in C#/ASP.NET, and I don't know how to send my data via POST to this remote PHP website. (Also note: the POST data I'm trying to send also contains a file uploader and a checkbox. my code is

string LiveUrl = "TestURL";

string resume = Server.MapPath("Doc1.docx");
string boundary = Guid.NewGuid().ToString();
HttpWebRequest request = HttpWebRequest.Create(LiveUrl)
    as HttpWebRequest;
CookieContainer cookies = new CookieContainer();
request.PreAuthenticate = true;

request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);

// request.PreAuthenticate = true; StringBuilder sb = new StringBuilder();

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterFirstName\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("Gary");
sb.AppendFormat("\r\n");


sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterLastName\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("five");
sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterEmailAddress\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("[email protected]");
sb.AppendFormat("\r\n");

Session["checked"] = true;

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterPassword\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("12345678");
sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterPasswordConfirm\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("12345678");
sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterHomeLocation\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("KA27 8AA");
sb.AppendFormat("\r\n");


sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterReferer\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("64");
sb.AppendFormat("\r\n");



sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterJobAlerts\"; checked=\""+Session["checked"].ToString()+"\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("");
sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterNewsletterOptions\"; checked=\"" + Session["checked"].ToString() + "\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("");
sb.AppendFormat("\r\n");

sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterAcceptTerms\"; checked=\"" + Session["checked"].ToString() + "\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat("");
sb.AppendFormat("\r\n");


sb.AppendFormat("--{0}--", boundary);
byte[] fulldata = Encoding.Default.GetBytes(sb.ToString());
request.ContentLength = fulldata.Length;
using (Stream sw = request.GetRequestStream())
{
    sw.Write(fulldata, 0, fulldata.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{

    HttpContext.Current.Response.Write(sr.ReadToEnd());

    //cvPostDataSecondTime();
}

}

View Answers









Related Tutorials/Questions & Answers:
How can I POST form data from a C#/ASP.NET to a remote website?
how to post data in mysql php form
Advertisements
How to send URL in Post request from local server to remote server?
How can I get specific data from JSON
How can I learn Data Science from scratch?
How Can I get ArrayList of Data from jsp with ajax request
How can I get specific data from JSON
how can i display a editable result of form?
php form post to mysql
How can I practice big data at home?
How can I become a good data scientist?
How can I get into big data?
How can I do data science course?
How can I become a data scientist in India?
How can I learn data science in India?
How can I start data analyst?
How can I start big data?
How can I become a data scientist for free?
How can I become a data scientist?
how to get a values from a multipart/form-data - JSP-Servlet
How do i validate form using javascript and send data to database?
How do I become a data analyst from scratch?
How do I start learning data science from scratch?
How can I start learning machine learning from scratch?
How retreive data from database without using post method in jsp - JSP-Servlet
How can I initialize the JSONArray and JSON object with data?
how to display output on jsp from while loop of action class....actually i am retreiving the post from posts column from my sql.plz help.thnkss
How can I become a data scientist in 3 months?
How can I learn Data Science for free medium?
How can I learn big data analytics online?
How can I become a data scientist after BSC?
How can I learn Data Science for free Quora?
How can I learn Hadoop big data online for free?
How can I start my career in data science?
How can i pass the valus from a JSP to the action class???
How can i read a file from nth line onwards in java
how can i run ASP.Net Server from netbeans IDE?
how can i create a data grid - Java Beginners
How can i download these java related materials from rose india
How can i Dispose Dialog box From LWUIT Component >
Want to learn java - How i can learn Java from scratch?
User Registration Form Using JSP(JspBeans) after that how i can insert in database
displaying data from ms excel in form.
How to downloads an application of the file from remote location?
how to open jdialog form in jinternalframe and transfer data from jdialog to jinternalform and vice-versa - Java Beginners
data science websites
How can I dynamically load user-entered data as content of jquery dialog?
post data
how i conditional access the data from database using combo box. - JSP-Servlet
best websites to learn data science

Ads