Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all, please help me for Posting data from one asp.net website to another asp.net website, i have use http-post method with cookies, but i cannont post data to file-uploader, please help me for posting the data to a page of another page (and at the another page at which i have to post data there are ,two textboxes,one file-uploader, two drop-downlists and one submit button).
thanks in advance.

My used Code----




C#
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;
using System.Text.RegularExpressions;

public partial class nurse : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        cvbrowserPostData();
    }
    public void cvbrowserPostData()
    {
        string ResumePath = Server.MapPath("Doc1.docx");
        string boundary = Guid.NewGuid().ToString();
        CookieContainer cookies = new CookieContainer();
        string url = "https://www.test.com/JobSeeker/PostNewResume.aspx?sslRedirectCnt=1";//not real url

        string resume = Server.MapPath("Doc1.docx");
     
        HttpWebRequest request = HttpWebRequest.Create(url)
            as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
        request.PreAuthenticate = true;
        request.CookieContainer = cookies;
        StringBuilder sb = new StringBuilder();

        //sb.AppendFormat("--{0}", boundary);
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("Content-Disposition: form-data; name=\"a_aid\"");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("\r\n");
        //sb.AppendFormat("cv-mate");
        //sb.AppendFormat("\r\n");


  

        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"ucResumeUpload:tbJobTitle\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("developer");
        sb.AppendFormat("\r\n");




        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"ucResumeUpload:tbLocation\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("sw13 9ru");
        sb.AppendFormat("\r\n");



        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"btnContinue\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Continue");
        sb.AppendFormat("\r\n");





        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"ucResumeUpload:newFileUpload:inputResumeUpload\"; 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; id=\"spanFilename\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("test.docx");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"ucResumeUpload:TargetLevel1:ddlLevel\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("SNCAREERLEVEL0");
        sb.AppendFormat("\r\n");



        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"ucResumeUpload:TargetSpecialism1:ddlSpecialism\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("SNSPECIALISM0");
        sb.AppendFormat("\r\n");

        sb.AppendFormat("--{0}", boundary);
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Content-Disposition: form-data; name=\"btnContinue\"");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("\r\n");
        sb.AppendFormat("Continue");
        sb.AppendFormat("\r\n");


        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());
            string ss = sr.ReadToEnd();
        }
    }
}
Posted
Updated 11-Jun-12 22:55pm
v5
Comments
Sergey Alexandrovich Kryukov 11-Jun-12 10:58am    
Where is the URI of "another ASP.NET application" page which is supposed to accept the post data?
--SA

1 solution

Please see my comment to the answer.

This is the first problem which catch an eye: where is the URI with the scheme "http" or "https"? You are using string url = "test@url.com"; which cannot be a Web page URI. You can upload the file with HttpWebRequest only if your have really existing Web page working through HTTP or HTTPS protocol on a currently running HTTP server; and this page should have the server-side code capable of receiving POST data and doing something with this data.

In the code you show, that "another ASP.NET application" simply does not exist.

Your question raises another question: do you really understand what HTTP does and how the Web works? It seems to me you need to learn about it; I don't even know what, maybe even learn from scratch.

Good luck with that,
—SA
 
Share this answer
 
v3
Comments
[no name] 12-Jun-12 0:32am    
hi thnx for comment , but sorry, i cannot mention our original url at which the data post, because some security reason,and i have used right and existing url,this is only test url which i have mention in above

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900