• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2357
  • Last Modified:

Problems with WebClient posting data

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 = "https://www.uhinet.com/batch/uploadbatchasync.asp";

                  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.

  • 2
1 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: http://www.dotnetforums.com/printthread.php?threadid=319
rshooper76Author Commented:
So, using htis method I format everything into a string, load it into a byte array and pass than in my UploadData call?
Yep, that's pretty much it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now