使用http post方法将数据发布到c#中的php网站

时间:2012-02-22 04:49:13

标签: c# asp.net http post checkbox

我正在尝试将数据发送到远程PHP网站。我想使用HTTP POST在会话中发送数据。问题是,我的网站是用C#/ ASP.NET构建的,我不知道如何通过POST将数据发送到这个远程PHP网站。 (另请注意:我正在尝试发送的POST数据还包含文件上传器和复选框。)

如何将表单数据从C#/ ASP.NET发布到远程网站?

我用过的代码 -

public void cvPostDataFirstTime()     {

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

1 个答案:

答案 0 :(得分:1)

您可以使用HttpWebRequest