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