Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT How can I POST form data from a C#/ASP.NET to a remote website?

Have Programming Question? Ask it here!
 
 
 


pardeep singh
How can I POST form data from a C#/ASP.NET to a remote website?
0 Answer(s)      2 years and 5 months ago
Posted in : Swing AWT


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("steve.102@fivesol.com");
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();
}

}


Advertisement
View Answers

Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com