PLEASE HELP: upload file from HTTP server using webrequest

  • Thread starter Thread starter amine
  • Start date Start date
A

amine

Hi all,

I have been developing an application for an IPAQ.
I am trying to write some code that will allow the user to
upload files to an HTTP server. I of course can't use the
webclient class since it is not supported by the compact
framework.
is there anyway I can use the webrequest and webresponse
classes to upload files from the httpserver.

I got this snippet of code from a site but it doenst work:

webrequest req = webrequest.create (url);
req.method = "put";
String temp_file_content ="file content";

byte[] encoded_content = Encoding.UTF8.GetBytes
(temp_file_content);
req.ContentLength = encoded_content.Length;

Stream request_stram = r4eq.GetRequestStream();
request_stream.Write (encoded_content, 0 ,
encoded_content.Length);
request_stram.Close();

PLEASE any help is very appreciated since I have looked
all over and couldnt find anything. I am very desperate

Thank You.
 
If you use the HttpWebRequest class (instead of WebRequest) and commit the
request by calling HttpWebRequest.GetResponse(), the following code should
compile and submit your post request.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "put";
String temp_file_content ="file content";

byte[] encoded_content = Encoding.UTF8.GetBytes
(temp_file_content);
req.ContentLength = encoded_content.Length;

Stream request_stream = req.GetRequestStream();
request_stream.Write (encoded_content, 0 ,
encoded_content.Length);
request_stream.Close();
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
resp.Close();

I have used similar code to successfully post in the past.

David Kline
Microsoft .NET Compact Framework
--------------------------------
This posting is provided “AS IS” with no warranties, and confers no
rights.

Please do not send email directly to this alias. This alias is for
newsgroup purposes only. To correspond with me directly, remove the
'online' from
my alias.
 
Back
Top