60734
Goto Top

TwitPic API Funktion Probleme mit Umlaute

Hallo Leute,

da ich für ein Programm, welches ich im Moment schreibe, eine Funktion brauche um Bilder auf TwitPic hochzuladen habe ich im internet erstmal gegoogelt.
Bin dann daraufhin auf folgende Funktion gestoßen:

 Public Function TwitPicUpload(ByVal Filename As String, ByVal Message As String, ByVal Username As String, ByVal Password As String, Optional ByVal Source As String = "") As String  

            Dim ReturnValue As String = String.Empty

            Try
                ' Get the file into an array of bytes  
                Dim BinaryData As Byte() = System.IO.File.ReadAllBytes(Filename)

                Dim boundary As String = Guid.NewGuid().ToString()
                Dim Header As String = String.Format("--{0}", boundary)  
                Dim Footer As String = String.Format("--{0}--", boundary)  

                ' Build the request  
                Dim Request As HttpWebRequest = DirectCast(WebRequest.Create("http://twitpic.com/api/uploadAndPost"), HttpWebRequest)  

                With Request
                    .PreAuthenticate = True
                    .AllowWriteStreamBuffering = True
                    .ContentType = String.Format("multipart/form-data; boundary={0}", boundary)  
                    .Method = "POST"  
                End With

                Dim FileContentType As String = String.Empty
                If Filename.ToUpper.EndsWith(".GIF") Then  
                    FileContentType = "image/gif"  
                ElseIf Filename.ToUpper.EndsWith(".JPG") Then  
                    FileContentType = "image/jpeg"  
                ElseIf Filename.ToUpper.EndsWith(".JPEG") Then  
                    FileContentType = "image/jpeg"  
                ElseIf Filename.ToUpper.EndsWith(".PNG") Then  
                    FileContentType = "image/png"  
                Else
                    Return String.Empty
                End If

                Dim FileHeader As String = [String].Format("Content-Disposition: file; name=""{0}""; filename=""{1}""", "media", Filename)  
                Dim FileData As String = Encoding.GetEncoding("iso-8859-1").GetString(BinaryData)  

                Dim Contents As New StringBuilder()
                With Contents
                    .AppendLine(Header)

                    .AppendLine(FileHeader)
                    .AppendLine([String].Format("Content-Type: {0}", FileContentType))  
                    .AppendLine()
                    .AppendLine(FileData)

                    .AppendLine(Header)
                    .AppendLine([String].Format("Content-Disposition: form-data; name=""{0}""", "username"))  
                    .AppendLine()
                    .AppendLine(Username)

                    .AppendLine(Header)
                    .AppendLine([String].Format("Content-Disposition: form-data; name=""{0}""", "password"))  
                    .AppendLine()
                    .AppendLine(Password)

                    If Source <> String.Empty Then
                        .AppendLine(Header)
                        .AppendLine([String].Format("Content-Disposition: form-data; name=""{0}""", "source"))  
                        .AppendLine()
                        .AppendLine(Source)
                    End If

                    If Not String.IsNullOrEmpty(Message) Then
                        .AppendLine(Header)
                        .AppendLine([String].Format("Content-Disposition: form-data; name=""{0}""", "message"))  
                        .AppendLine()
                        .AppendLine(Message)
                    End If

                    .AppendLine(Footer)

                    Dim Bytes As Byte() = Encoding.GetEncoding("iso-8859-1").GetBytes(Contents.ToString())  
                    Request.ContentLength = Bytes.Length

                    Using requestStream As Stream = Request.GetRequestStream()
                        requestStream.Write(Bytes, 0, Bytes.Length)

                        Using Response As HttpWebResponse = DirectCast(Request.GetResponse(), HttpWebResponse)
                            Using Reader As New StreamReader(Response.GetResponseStream())
                                Dim Result As String = Reader.ReadToEnd()

                                Dim ResponseDoc As New XmlDocument
                                ResponseDoc.LoadXml(Result)
                                Dim rsp As XmlNode = ResponseDoc.SelectSingleNode("//rsp")  
                                If rsp.Attributes("status").Value = "ok" Then  
                                    ReturnValue = ResponseDoc.SelectSingleNode("//mediaurl").Value  
                                End If
                            End Using
                        End Using
                    End Using
                End With

            Catch ex As Exception
                ' Deliberately doing nothing here.  
                ' We just want to swallow the exception so that the application doesn't see an exception  

            End Try

            Return ReturnValue
        End Function


Da twitter ja aber will bzw. ab dem 1. 7. soweit ich das lesen konnte vorschreibt, dass man bei api's sich zukünftig mit oauth / xoauth anmeldet, habe ich die funktion umgebastelt:

Public Function uploadPic(ByVal pic As Byte(), ByVal filename As String, ByVal consumer_token As String, ByVal consumer_secret As String, ByVal oath_token As String, ByVal oath_secret As String, ByVal message As String)

    
        Dim encoding As String = "iso-8859-1"  



        Dim gui As String = Guid.NewGuid().ToString()


        Dim head As String = String.Format("--{0}", gui)  
        Dim foot As String = String.Format("--{0}--", gui)  

        
        Dim contents As StringBuilder = New StringBuilder()

        'twitpic id   
        contents.AppendLine(head)
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "key"))  
        contents.AppendLine()
        contents.AppendLine("dad3e3d0c28210e2388946bf787e9d28")  

        'consumer token  
        contents.AppendLine(head)
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "consumer_token"))  
        contents.AppendLine()
        contents.AppendLine(consumer_token)

        'consumer secret  
        contents.AppendLine(head)
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "consumer_secret"))  
        contents.AppendLine()
        contents.AppendLine(consumer_secret)

        'oath token  
        contents.AppendLine(head)
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "oauth_token"))  
        contents.AppendLine()
        contents.AppendLine(oath_token)



        'oeath secret  
        contents.AppendLine(head)
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "oauth_secret"))  
        contents.AppendLine()
        contents.AppendLine(oath_secret)

        'nachricht  
        contents.AppendLine(head)
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "message"))  
        contents.AppendLine()
        contents.AppendLine(message)


        contents.AppendLine(head)
        contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "post_photo"))  
        contents.AppendLine()
        contents.AppendLine("1")  


        'Header schreiben  
        contents.AppendLine(head)

        'Bildinformationen schreiben, Bildkopf und die Binärdaten  
        Dim fileHeader As String = String.Format("Content-Disposition: file; name=""{0}""; filename=""{1}""", "media", filename)  
        Dim fileData As String = System.Text.Encoding.GetEncoding(encoding).GetString(pic)
        'Informationen zu dem Übergebenen Dateityp schreiben  
        contents.AppendLine(fileHeader)
        contents.AppendLine(String.Format("Content-Type: {0}", "image/jpeg"))  
        contents.AppendLine()
        contents.AppendLine(fileData)


        'Durch schreiben des footers signalisieren dass keine Daten mehr kommen  
        contents.AppendLine(foot)




        'Stream Reader zum lesen der Antwort von Twitpic  
        Dim reader As StreamReader
        Dim result As String
        Dim response As HttpWebResponse

        'Einen Webrequest zu der TwitPic API erstellen  
        Dim request As HttpWebRequest = WebRequest.Create("http://api.twitpic.com/1/uploadAndPost.format")  

        'Die Auflagen die in der API beschreiben sind erfüllen:  
        'API-Zitat:  
        '---  
        'Fields to post in:  
        '(post data should be formatted as multipart/form-data)  
        '---  
        request.ContentType = String.Format("multipart/form-data; boundary={0}", gui)  
        request.Method = "POST"  

        'Die Daten die noch im Stringbuilder als String vorliegen  
        'in das byte (Binär)-Format umwandeln, damit die API diese annimt  

        Dim bytes As Byte() = System.Text.Encoding.GetEncoding(encoding).GetBytes(contents.ToString())

        request.ContentLength = bytes.Length

        'Einen Stream aus dem WebRequest erstellen  
        Dim writer As Stream = request.GetRequestStream()

        'Die Binären Daten in den Strom schreiben  
        writer.Write(bytes, 0, bytes.Length)
        response = request.GetResponse()

        'Antwort von Twitpic empfangen  
        reader = New StreamReader(response.GetResponseStream)
        result = reader.ReadToEnd

        reader.Close()


    End Function


Diese funktioniert auch einwandrei.

Problem nur, dass wenn in der variable message irgendein Umlaut wie ö ä oder ü enthalten ist, ab dem Punkt die Nachricht abgeschnitten wird.


WEnn ich z.B. ein Bild wegsende mit der Nachricht "Hallo, das hier ist ein Kopfhörer", kommt am ende "Hallo, das hier ist ein Kopfh" raus.

Warum ist das so?


Vielen Dank schonmal & Gruss

Content-Key: 145899

Url: https://administrator.de/contentid/145899

Printed on: April 26, 2024 at 00:04 o'clock