Hi I am not sure if I am posting this question in the right section, but here it goes. I am trying to create an HttpWebRequest that uploads a file. I have an existing applicationt that works in Perl CGI that accomplishes this by doing the following as an example:
POST 'http://www.perl.org/survey.cgi', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => 'gisle@aas.no', gender => 'M', born => '1964', init => ["$ENV{HOME}/.profile"], ] the name/value pair 'init' is where the file is located. What would
be the equivalent of this in ASP.NET. I am using C#. Thanks Babak
There are a couple ways you can do it. One of the easier, built-in ways is to use the WebClient class to upload simple things (files, values, etc.). But because you have multiple values as well as a file, i'd upload using the UploadValues method of the WebClient
class: // set uri of file to post to string uriString = "http://www.perl.org/survey.cgi"; // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Create a new NameValueCollection instance to hold some custom parameters to be posted
NameValueCollection myNameValueCollection = new NameValueCollection(); // get values here, email, gender, born, init etc. // Add necessary parameter/value pairs to the name/value container. myNameValueCollection.Add("email",email); myNameValueCollection.Add("gender",gender);
myNameValueCollection.Add("born",born); myNameValueCollection.Add("init",init); // Upload the NameValueCollection. byte[] responseArray = myWebClient.UploadValues(uriString,"POST",myNameValueCollection);
Note: when using this, be sure to include a using statement at the top:
using System.Collections.Specialized; However, if init is the location of the file on the user's machine, you won't be able to access it. You'll need to encode the byte array of the file (use Encoding.Unicode) and put that in the namevaluecollection
(it only takes strings). Then on the reciever page, grab the string and decode it back to a byte array and output the file.
Thanks for the quick response. I actually figured out another way to do this, a bit mroe difficult than what u mentioned but it works. Here is what i did: ==========Code Begin ===================== System.Uri uri = new Uri(msp_url); string dataBoundary = "--xyz";
System.Net.HttpWebRequest webRqst = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri); //add existing cookies to the webrequest. webRqst.CookieContainer = new CookieContainer(); webRqst.CookieContainer.Add(mspCookies); //create the WebRequest
Content-Body manually for file upload. webRqst.ContentType = "multipart/form-data; boundary=xyz"; webRqst.Method = "POST"; webRqst.KeepAlive = true; //get the file content that will be uploaded. StreamReader sr = new StreamReader(filepath); string FileData
= sr.ReadToEnd(); // test data to send. sr.Close(); //make a string to store the content. StringBuilder DataString = new StringBuilder(); DataString.Append(dataBoundary + "\r\n"); DataString.Append("Content-Disposition: form-data; name=\"filename\"\r\n\r\n"
+ "test.txt\r\n"); DataString.Append(dataBoundary + "\r\n"); DataString.Append("Content-Disposition: form-data; name=" + "\"submitFile\"" + "; filename=" + "\"" + filename + "\"" + "\r\n"); DataString.Append("Content-Type: text/plain\r\n\r\n"); DataString.Append(FileData);
DataString.Append(dataBoundary + "--\r\n"); byte []Postdata = System.Text.Encoding.Default.GetBytes(DataString.ToString()); //change the requests content length to the size of data being posted in request. webRqst.ContentLength = Postdata.Length; //get the
response stream from the webrequest and write the data to be posted to the //Request Stream Stream tempStream = webRqst.GetRequestStream(); tempStream.Write(Postdata,0,Postdata.Length); tempStream.Flush(); tempStream.Close(); //change certificate policy to
always allow. System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); System.Net.HttpWebResponse webRsp = (System.Net.HttpWebResponse)webRqst.GetResponse(); ==========Code End===================== What I have done here is basically get the webrequests
Stream and write the HttpRequest header along with the file contents to it. It works well. Babak
None
0 Points
2 Posts
Creating HttpWebRequest
Apr 06, 2004 02:48 PM|babakm|LINK
Member
20 Points
491 Posts
Re: Creating HttpWebRequest
Apr 06, 2004 04:48 PM|chapel21|LINK
// set uri of file to post to string uriString = "http://www.perl.org/survey.cgi"; // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Create a new NameValueCollection instance to hold some custom parameters to be posted NameValueCollection myNameValueCollection = new NameValueCollection(); // get values here, email, gender, born, init etc. // Add necessary parameter/value pairs to the name/value container. myNameValueCollection.Add("email",email); myNameValueCollection.Add("gender",gender); myNameValueCollection.Add("born",born); myNameValueCollection.Add("init",init); // Upload the NameValueCollection. byte[] responseArray = myWebClient.UploadValues(uriString,"POST",myNameValueCollection);
Note: when using this, be sure to include a using statement at the top:using System.Collections.Specialized;
However, if init is the location of the file on the user's machine, you won't be able to access it. You'll need to encode the byte array of the file (use Encoding.Unicode) and put that in the namevaluecollection (it only takes strings). Then on the reciever page, grab the string and decode it back to a byte array and output the file.None
0 Points
2 Posts
Re: Creating HttpWebRequest
Apr 06, 2004 07:08 PM|babakm|LINK