views:

337

answers:

1

How to create an POST with the HttpWebRequest Class in VB.net (2008)? I can find exemples with text only but not with text and files. Thanks.

A: 

You should be able to read the file in a buffer, and upload it to the server. It is not that difficult.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method  "POST";
Stream fileStream = File.OpenRead("filename");
byte [] buffer = new byte[1024];
Stream reqStream = request.GetRequestStream();

int read = fileStream.Read(buffer, 0, buffer.Length);
while(read > 0)
{
reqStream.Write(buffer,0,read);
read  fileStream.Read(buffer, 0, buffer.Length);
}

reqStream.Close();
fileStream.Close();

// now get the response...

Does that help?

feroze
Thank you, but an example in vb.net would help me more.
tobiasre
I am sure you can find a VB.net sample on MSDN. In any case, you can make it even simpler. Just use WebClient.WebClient client = new WebClient();client.UploadFile(...)
feroze
Yes, WebClient() is simpler, but it isn´t possible to make a POST with text + files (in one POST) in this Class.
tobiasre
MSDN: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(VS.71).aspx I think its a good help but no example .
tobiasre