Uploading a binary file as part of a POST WebRequest

  • Follow


Hello Everyone,

I've been searching this out for almost three weeks and I simply can't find 
an answer anywhere. I see a lot of people asking about it but nobody seems 
to know the answer.  Since I've gotten a few questions answered on this 
group before, I'm going to ask the question again here. It's about uploading 
a file as part of a WebRequest.

My application is a mobile one which means I'm using the .NET Compact 
Framework and thus, don't have access to the WebClient.UploadFile method. 
But even if I did, that wouldn't work here since I need to pass several 
parameters along and UploadFile() doesn't allow you to do that anyway.

The documentation to the API I'm working with tells me to create a  POST 
request and pass the following data to the API:

Subject     - which is the subject of the file being uploaded
Recipient   - the person receiving notification
Source      - The raw binary data of the file to send

Obviously, since the API calls for this, this can be done, but HOW do I do 
it? Note that I need to be able to do this without any third party 
assemblies. It has to be a standard POST using WebRequest.

Can anyone give me some direction?

Thanks!
Anthony 

0
Reply Anthony 2/1/2010 7:13:50 AM

Hello,

Then you could use HttpWebRequest (which seems to be available in the 
Compact Framework) and handle the request detail yourself.

See :
http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data 
or
http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx

(C# but should translate easily to VB).

Also you could create an APSX page that would accept something similar to 
the API you are targetting allowing :
- to see what is the correct format by looking at what is sent when  you 
post from the browser (using either the Fiddler tool client side or 
Request.SaveAs server side)
- and from there to test that your code ultimately creates a request that is 
similar to the one you got above using your browser

--
Patrice


 


0
Reply Patrice 2/1/2010 4:39:59 PM


Hello Anthony


probably the API works with multipart form data


I have once written an example here in this group for someone who needed 
this for the twitpic API
dig dig dig dig ,,,,,,,,


And here it is  , the code sends a picture and some text to a twitter 
account with the webrequest  as you see you can send parameters as form data
see if you can convert it to your needs or maybe it gives you some ideas on 
how to do what you want .


<<<<
'Michel Posseth [MCP]
'Created : 23-02-2009
'i...@VbDotNetCoder.com
Imports System
Imports System.IO
Imports System.Net
Imports System.Xml
Imports System.Web
Imports System.Collections.Generic
Imports System.Text
Namespace VBDC
Public Class ClsTwitPic
Public Function SendPicToTwitPic(ByVal userName As String, ByVal
password As String, ByVal Message As String, ByVal ImgPath As String) As
String
Using fs As System.IO.FileStream = New FileStream(ImgPath,
FileMode.Open, FileAccess.Read)
Dim ImgData As Byte() = New Byte(fs.Length - 1) {}
fs.Read(ImgData, 0, ImgData.Length)
Return SendPicToTwitPic(userName, password, ImgData,
Message)
End Using
End Function
Private Function SendPicToTwitPic(ByVal UserName As String, ByVal
PassWord As String, ByVal Image As Byte(), Optional ByVal Message As String
= "", Optional ByVal Uri As String = "http://twitpic.com/api/uploadAndPost")
As String
Dim ret As String = String.Empty
Dim request As HttpWebRequest =
DirectCast(WebRequest.Create(Uri), HttpWebRequest)
request.Method = "POST"
request.Timeout = 30000


If Not String.IsNullOrEmpty(UserName) AndAlso Not
String.IsNullOrEmpty(PassWord) Then


Dim boundary As String = System.Guid.NewGuid().ToString()


request.Credentials = New NetworkCredential(UserName,
PassWord)


request.Headers.Add("Accept-Language",
request.PreAuthenticate = True
request.ContentType =
String.Format("multipart/form-data;boundary={0}", boundary)


Dim header As String = String.Format("--{0}", boundary)
Dim footer As String = header & "--"


Dim contents As New StringBuilder()


contents.Append(header)
contents.Append(vbCrLf)
contents.Append("Content-Disposition:
form-data;name=""username""" & vbCrLf)
contents.Append(vbCrLf)
contents.Append(UserName)
contents.Append(vbCrLf)


contents.Append(header)
contents.Append(vbCrLf)
contents.Append("Content-Disposition:
form-data;name=""password""" & vbCrLf)
contents.Append(vbCrLf)
contents.Append(PassWord)
contents.Append(vbCrLf)


If Not String.IsNullOrEmpty(Message) Then
contents.Append(header)
contents.Append(vbCrLf)
contents.Append("Content-Disposition:
form-data;name=""message""" & vbCrLf)
contents.Append(vbCrLf)
contents.Append(Message)
contents.Append(vbCrLf)
End If


contents.Append(header)
contents.Append(vbCrLf)
contents.Append("Content-Disposition:form-data;
name=""media"";filename=""image.jpg""" & vbCrLf)
contents.Append("Content-Type: image/jpeg" & vbCrLf)
contents.Append(vbCrLf)


Dim Einde As String = vbCrLf & header & vbCrLf
Dim BodyBytes As Byte() =
Encoding.UTF8.GetBytes(contents.ToString())
Dim footerBytes As Byte() = Encoding.UTF8.GetBytes(Einde)


request.ContentLength = BodyBytes.Length + Image.Length +
footerBytes.Length


Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(BodyBytes, 0, BodyBytes.Length)
requestStream.Write(Image, 0, Image.Length)
requestStream.Write(footerBytes, 0, footerBytes.Length)
requestStream.Flush()
requestStream.Close()
Using response As WebResponse = request.GetResponse()
Using reader As New
StreamReader(response.GetResponseStream())
ret = reader.ReadToEnd()
End Using
End Using
End Using
End If


Return ret
End Function


End Class


End Namespace




Usage


Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click


Dim TwitPic As New VBDC.ClsTwitPic


Debug.WriteLine(TwitPic.SendPicToTwitPic("YourUsername",


End Sub


End Class


the response i got was OK




when viewing  my twitter  account ( michel1973 )  i found my send picture
and the text i entered  so it works


Regards


Michel Posseth [MCP] , VBDC
http://www.vbdotnetcoder.com/

HTH



Michel Posseth



>>>










"Anthony Papillion" <papillion@gmail.com> schreef in bericht 
news:e06Sa4woKHA.1556@TK2MSFTNGP05.phx.gbl...
> Hello Everyone,
>
> I've been searching this out for almost three weeks and I simply can't 
> find an answer anywhere. I see a lot of people asking about it but nobody 
> seems to know the answer.  Since I've gotten a few questions answered on 
> this group before, I'm going to ask the question again here. It's about 
> uploading a file as part of a WebRequest.
>
> My application is a mobile one which means I'm using the .NET Compact 
> Framework and thus, don't have access to the WebClient.UploadFile method. 
> But even if I did, that wouldn't work here since I need to pass several 
> parameters along and UploadFile() doesn't allow you to do that anyway.
>
> The documentation to the API I'm working with tells me to create a  POST 
> request and pass the following data to the API:
>
> Subject     - which is the subject of the file being uploaded
> Recipient   - the person receiving notification
> Source      - The raw binary data of the file to send
>
> Obviously, since the API calls for this, this can be done, but HOW do I do 
> it? Note that I need to be able to do this without any third party 
> assemblies. It has to be a standard POST using WebRequest.
>
> Can anyone give me some direction?
>
> Thanks!
> Anthony 

0
Reply Michel 2/1/2010 6:01:34 PM

"Patrice" <http://www.chez.com/scribe/> wrote in message 
news:uDCQz01oKHA.1544@TK2MSFTNGP02.phx.gbl...
> Hello,
>
> Then you could use HttpWebRequest (which seems to be available in the 
> Compact Framework) and handle the request detail yourself.
>
> See :
> http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data 
> or
> http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx
>
> (C# but should translate easily to VB).
>
> Also you could create an APSX page that would accept something similar to 
> the API you are targetting allowing :
> - to see what is the correct format by looking at what is sent when  you 
> post from the browser (using either the Fiddler tool client side or 
> Request.SaveAs server side)
> - and from there to test that your code ultimately creates a request that 
> is similar to the one you got above using your browser
>
> --
> Patrice

As usual, thank you for your help! Checking out the links now :-)

Anthony 

0
Reply Anthony 2/1/2010 10:47:10 PM

"Michel Posseth [MCP]" <msdn@posseth.com> wrote in message 
news:0A941F23-2DA2-4348-9ED4-4D02096CE317@microsoft.com...
> Hello Anthony
>
>
> probably the API works with multipart form data
>
>
> I have once written an example here in this group for someone who needed 
> this for the twitpic API
> dig dig dig dig ,,,,,,,,
>
>
> And here it is  , the code sends a picture and some text to a twitter 
> account with the webrequest  as you see you can send parameters as form 
> data
> see if you can convert it to your needs or maybe it gives you some ideas 
> on how to do what you want .

Thank you sir! Exactly what I'm looking for. You just saved me a bunch of 
work and puzzlement!

Anthony 

0
Reply Anthony 2/1/2010 10:47:53 PM

4 Replies
2374 Views

(page loaded in 0.084 seconds)




Reply: