Hello Gillard
forget the previous post it doesn`t work , i guess that this is the
problem when you answer a lot of postings on an old computer without a
development environment installed
i forgot that the namevalue collection only accepts string data :-(
But hey you are lucky for 2 reassons
1. my old computer has crashed and i am currently behind my business
laptop wich does have a dev environment
2. i had some spare time and wanted to investigate that twitter thingy
as i like the concept of your task
After some investigating on the web , i found that there is not so much
example code that work on win clients ( actually i did not find one )
however i did find some ASP.Net C# and PHP code wich i harvested from
the web and did my own thingy with
So i created a twitter account and tested the result and to my own
surprise it worked flawless ;-)
so here it is
<CLASS CODE >
'Michel Posseth [MCP]
'Created : 23-02-2009
'(e-mail address removed)
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",
"vb,en-us;q=0.7,en;q=0.3")
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
</CLASS CODE>
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",
"YourPassword", "some text ", "C:\FullPathToPic.jpg"))
End Sub
End Class
the response i got was
<?xml version="1.0" encoding="UTF-8"?>
<rsp status="ok">
<statusid>1242482808</statusid>
<userid>21698827</userid>
<mediaid>1nmne</mediaid>
<mediaurl>
http://twitpic.com/1nmne</mediaurl>
</rsp>
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/
Gillard said:
I do not understand where I put the image data???
Vals.Add( ???
or
oWc.Headers.Add ???
and if it is in oWc.Headers then
what is vals for ???
Michel Posseth said:
Aha
http://www.twitpic.com/api.do
Well that should be no problem
this example might give you an idea
Dim oWc As New System.Net.WebClient()
oWc.Headers.Add("Content-Type", "your transfer type")
Dim Vals As New
System.Collections.Specialized.NameValueCollection()
Vals.Add("bla1", "value of 1")
Vals.Add("bla2", "value of 2")
Dim byteBuffer As Byte()
byteBuffer =
oWc.UploadValues("
http://www.example.com/justaname.aspx", "POST", Vals)
Dim sBuffer As String=
System.Text.Encoding.Default.GetString(byteBuffer)
HTH
Michel
"Michel Posseth [MCP]" <
[email protected]> schreef in bericht
Maybe you can provide some more info
As wich of the API methods of twitter you want to call to perfom this
action
regards
Michel
"Gillard" <gillard_georges@@@@@@@@@hotmail.com> schreef in bericht
well it may work as is for a Web App but I'm on a Desktop app
I'm making a twitter client and the multipart/form data is for to post
pictures on twitpic.com
sorry I should have sayed that was for a desktop app
"Cor Ligthert[MVP]" <
[email protected]> a écrit dans le message
de groupe de discussion : (e-mail address removed)...
Gillard,
Simple start a webapplication and than a form.
Drag 3 textboxes on it, and you have your page.
In the IsPostBack event you look if the username and password are
entered.
(You can also see this at client side, but then you need some non
VB.Net code)
Cor
hi,
how do I have to do to post "multipart/form-data" in vb.net
here is the data I have to post togueter
----Begin----------------------------
media (required) - Binary image data - username (required) -
username - password (required) - password - message (optional) -
Message
-----End----------------
how do I format it?
I really do not know how wich framework class to use
System.Net.HttpWebRequest ?
how do I handle the response
please , any idea to help me ?