Hi, I am trying to posting the data asp.net to Php website but not successful. here is my code public void MyPostData() { string boundary = Guid.NewGuid().ToString(); HttpWebRequest request = HttpWebRequest.Create("http://www.abc.com/register.php") as HttpWebRequest; request.Method = "POST"; request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); request.PreAuthenticate = true; request.KeepAlive = 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("S"); 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("abc@gmail.com"); sb.AppendFormat("\r\n"); 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("123456"); 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("123456"); 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("b27 7xu"); 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("10"); sb.AppendFormat("\r\n"); sb.AppendFormat("--{0}", boundary); sb.AppendFormat("\r\n"); sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterJobAlerts\""); sb.AppendFormat("\r\n"); sb.AppendFormat("\r\n"); sb.AppendFormat("1"); sb.AppendFormat("\r\n"); sb.AppendFormat("--{0}", boundary); sb.AppendFormat("\r\n"); sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterNewsletterOptions\""); sb.AppendFormat("\r\n"); sb.AppendFormat("\r\n"); sb.AppendFormat("1"); sb.AppendFormat("\r\n"); sb.AppendFormat("--{0}", boundary); sb.AppendFormat("\r\n"); sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterAcceptTerms\""); sb.AppendFormat("\r\n"); sb.AppendFormat("\r\n"); sb.AppendFormat("1"); sb.AppendFormat("\r\n"); sb.AppendFormat("--{0}", boundary); sb.AppendFormat("\r\n"); sb.AppendFormat("Content-Disposition: form-data; name=\"XiFormSubmit\""); sb.AppendFormat("\r\n"); sb.AppendFormat("\r\n"); sb.AppendFormat("Submit"); 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()); } } Can any body help to sort my issue.... Many Thanks Gary
My Problem has been resolved. Php Website using the cookies concept so i have added the cookie parameter and it works . CookieContainer cookies = new CookieContainer(); HttpWebRequest request = HttpWebRequest.Create("http://www.abc.com/register.php") as HttpWebRequest; request.Method = "POST"; request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); request.PreAuthenticate = true; // request.KeepAlive = true; request.CookieContainer = cookies;
One more site i am trying to integrate the same functionality but facing the issue. Application is not accepting the values. Another website(PHP) using a the session concept. I am just trying to register the user profile which could be completed in two steps. Any body can sort out this issue. Thanks in Advanced.
Partn32 is a bad dude. I bought, paid and he never came through with logo contest from this string http://forums.digitalpoint.com/showthread.php?t=2405998 Read his Itrader account.
Get the API spec for the site you want to post to, then you'll see exactly what the site needs. You can't just guess these things.
Hi Garry, Even, I have to do the same task but in different manner, As, We have one application in .NET and other is in PHP, In both applications, we create profiles. But, Now the requirement is when we create profile in our .NET application then same details will be inserted in our PHP application i.e. in one single click same data inserted in both .NET and php. But, before inserting any record we need to Log-in the site and then data will be inserted. I try to use your code but no success in it. Please let me where I am wrong. string boundary = Guid.NewGuid().ToString(); CookieContainer cookies = new CookieContainer(); HttpWebRequest request = HttpWebRequest.Create("http://64.50.177.73/") as HttpWebRequest; request.Method = "POST"; request.CookieContainer = cookies; StringBuilder sb = new StringBuilder(); sb.AppendFormat("--{0}", boundary); sb.AppendFormat("\r\n"); sb.AppendFormat("Content-Disposition: form-data; name=\"login\""); sb.AppendFormat("\r\n"); sb.AppendFormat("\r\n"); sb.AppendFormat("Admin"); sb.AppendFormat("\r\n"); sb.AppendFormat("--{0}", boundary); sb.AppendFormat("\r\n"); sb.AppendFormat("Content-Disposition: form-data; name=\"pass\""); sb.AppendFormat("\r\n"); sb.AppendFormat("\r\n"); sb.AppendFormat("P@ssword"); 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()); } Thanks.
Hi Rukbat, I have an PHP api with me, how can access it (all it's methods) in my asp.net code? Thanks.