Problems with WebClient posting data

Posted on 2006-05-03
Last Modified: 2009-07-29
I am trying to write a custom program to upload a flat file to one of our providers.  Their specifications state that I need to use a multipart/form-data POST.  I have another part of the coded that uses an application/x-www-fomr-urlencoded that is working just.  How do I get my WebClient to use the multipart/form-data POST and how to I set the boundary value.  Below is a sample of my code.  I do get a response back from the server saying the didn't recieve any data.

                                                lblStatus.Text = "Uploading File, Please Wait.";
                  string strFileName = objOpenFile.FileName.ToString();
                  int intFac = (int)cboFacility.SelectedValue;
                  UhinInfo objUhin = new UhinInfo( intFac );

                  string uriString = "";

                  WebClient myWebClient = new WebClient();                  

                  NameValueCollection myNameValueCollection = new NameValueCollection();

                  string temp = objUhin.UHINID.ToString();                  
                  myNameValueCollection.Add("UID", objUhin.UHINID.ToString() );
                  myNameValueCollection.Add("PWD", objUhin.UHINPassword.ToString() );
                  myNameValueCollection.Add("TPN", objUhin.UHINTPID.ToString() );
                  myNameValueCollection.Add("task", "postdata" );
                  StreamReader objTranFile = File.OpenText(objOpenFile.FileName.ToString());
                  string strFile = objTranFile.ReadToEnd();
                  myNameValueCollection.Add("f1", strFile );
                  byte[] responseArray = myWebClient.UploadValues(uriString, "POST", myNameValueCollection);

                  string strResponse = Encoding.ASCII.GetString(responseArray);
                  lblStatus.Text = "File Uploaded";

The provider suggested that I add myWebClient.Headers.Add("Content-Type","multipart/form-data");  to my code, you will notice this is commented out.  I get the following error when that is added when the myWebClient.Headers.Add("Content-Type","multipart/form-data"); runs.

   An unhandled exception of type 'System.Net.WebException' occurred in system.dll

   Additional information: The Content-Type header cannot be changed from its default value for this request.

Question by:rshooper76
    LVL 3

    Accepted Solution

    You need to use the UploadData method of the WebClient class instead of UploadValues since UploadValues imposes a special formatting on the uploaded information that's inconsistent with the multipart/form-data content type. You also need to format your values manually into a multipart-compliant string.

    See this URL for more information:
    LVL 9

    Author Comment

    So, using htis method I format everything into a string, load it into a byte array and pass than in my UploadData call?
    LVL 3

    Expert Comment

    Yep, that's pretty much it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now