Click here to Skip to main content
11,492,523 members (60,025 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# checkbox Post HTTP
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.)

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

My Code is:----

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
 
public partial class Default13 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           
            PostDataFirstTimess();
        }
    }
 
  
   
 
    #region First
 
    public void PostDataFirstTime()
    {
       
        string LiveUrl = "http://www.abc.co.uk/Jobsite/Candidates/Register.php";
        //bool accept = this.CandidateRegisterAcceptTerms.Checked;
        //bool allert = this.CandidateRegisterJobAlerts.Checked;
        //bool news = this.CandidateRegisterNewsletterOptions.Checked;

        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);
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("Content-Disposition: form-data; name=\"XiFormSubmit\"");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("Next Step");
        //sb.AppendFormat("\r\n");

        //sb.AppendFormat("--{0}", boundary);
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("Content-Disposition: form-data; name=\"message\"");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat(this.txtComments.Text);
        //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()))
        {
            //string sss = sr.ReadToEnd();
            HttpContext.Current.Response.Write(sr.ReadToEnd());
 
            PostDataSecondTime();
        }
    }
    #endregion
 
    #region Post Method For 2nd time
    public void PostDataSecondTime()
    {
        string resume = Server.MapPath("Doc1.docx");
        string boundary = Guid.NewGuid().ToString();
        HttpWebRequest request = HttpWebRequest.Create("http://www.abc.co.uk/Jobsite/Candidates/Register.php")
            as HttpWebRequest;
        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=\"CandidateRegistrationProfileName\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Resume");
        sb.AppendFormat("\r\n");
 

        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateWorkLocations[]\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("578");
        sb.AppendFormat("\r\n");
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationSkills\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Asp");
        sb.AppendFormat("\r\n");
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationWorkExperience\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("4");
        sb.AppendFormat("\r\n");
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationCareerToDate\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("12-2-2012");
        sb.AppendFormat("\r\n");
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationDesiredJobTitles\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Accountant");
        sb.AppendFormat("\r\n");
 
      
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationCVUpload\"; filename=\"" + Path.GetFileName(resume) + "\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Type: application/msword");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        using (FileStream fs = new FileStream(resume, FileMode.Open, FileAccess.Read))
        {
            byte[] contents = new byte[fs.Length];
            fs.Read(contents, 0, contents.Length);
            sb.Append(Encoding.Default.GetString(contents));
        }
        sb.AppendFormat("\r\n");
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationJobSectors[]\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("721");
        sb.AppendFormat("\r\n");
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationSalary\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("2");
        sb.AppendFormat("\r\n");
 
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegistrationEmploymentTypes[]\"");
        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=\"CandidateRegistrationHeadhunted\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Checked");
        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);
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("Content-Disposition: form-data; name=\"message\"");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat(this.txtComments.Text);
        //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());
          
        }
    }
    #endregion
 
}
Posted 21-Feb-12 3:30am
Edited 21-Feb-12 19:15pm
v2
Comments
krumia at 21-Feb-12 10:19am
   
If your design is correct, you shouldn't worry about the implementation language of your website when uploading a file to it. If you implementation depends on it, think again. Because there should be something wrong.
SAKryukov at 21-Feb-12 10:39am
   
You are right, but OP does not explain any problem (except pretty bad code).
--SA
Pardeep singh from Chandigarh at 21-Feb-12 23:18pm
   
hi, i can'nt understand what is the problem ,i want to simply send this information to a php website for fill the forms, using http post method in asp.net
SAKryukov at 21-Feb-12 10:38am
   
Not clear what is the problem. First of all, where is your PHP code? Why would you do all this posting code at all? Why not creating a form with all those names and data? Is this client the only client of the PHP part?
--SA
krumia at 21-Feb-12 10:49am
   
SA, did you see the hardcoded URLs? I think he's trying to bulk register users in a site. (i.e. he's trying to build a bot) :D
SAKryukov at 21-Feb-12 12:42pm
   
You, I can see just one, repeated... maybe this is good that the code is that bad... :-)
--SA
Pardeep singh from Chandigarh at 21-Feb-12 23:17pm
   
hi, sorry if you are not understand my code, please can you tell me another way for posting data to php website, the main problem is that ,at the first form of the website contain checkbox, and textbox, and then click on submit button then we go to a next form at which a fileupload and many text box to fill data, the php website is the client site, and i want to fill these form from the my site which is made in C#, thnx in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 399
1 Maciej Los 266
2 KarstenK 210
3 CPallini 208
4 Sascha Lefèvre 195
0 Sergey Alexandrovich Kryukov 9,943
1 OriginalGriff 8,610
2 Sascha Lefèvre 3,609
3 Maciej Los 3,372
4 Richard Deeming 2,440


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 22 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100