• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2115
  • 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();                  
                  //myWebClient.Headers.Add("Content-Type","multipart/form-data");

                  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.


0
rshooper76
Asked:
rshooper76
  • 2
1 Solution
 
ChipzterCommented:
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
0
 
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?
0
 
ChipzterCommented:
Yep, that's pretty much it.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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