Need Help.. Picture uploader function for windows mobile 6 net cf 3.5
all runs perfect but the thing that cost me my last nervs is the getstring part .. that function is wonderfull but i need a way to find an alternative for the getstring problem.. with getstring(pic,0,pic.length) i became an error.. and find no way out big thx for help
hers the code:
Public Function uploadPic(ByVal pic As Byte(), ByVal filename As String, ByVal user As String)
Dim encoding As String = "iso-8859-1"
'Erzeugen einer einzigartigen identifikation
Dim gui As String = Guid.NewGuid().ToString()
'Diese wird für den Header und den footer der Daten benötigt
Dim head As String = String.Format("--{0}", gui)
Dim foot As String = String.Format("--{0}--", gui)
'Einen Stringbuilder erstellen, in dem wir nun bequem die
'benötigten POST Daten speichern können
Dim contents As StringBuilder = New StringBuilder()
'Benutzerdaten schreiben (benutzername)
contents.AppendLine(head)
contents.AppendLine(String.Format("Content-Disposition: form-data; name=""{0}""", "username"))
contents.AppendLine()
contents.AppendLine(user)
'Header schreiben
contents.AppendLine(head)
'Bildinformationen schreiben, Bil开发者_如何学Pythondkopf und die Binärdaten
Dim fileHeader As String = String.Format("Content-Disposition: file; name=""{0}""; filename=""{1}""", "media", user & ".jpg")
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)
'MessageBox.Show(contents.ToString)
'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://urspacecity.de/uploadpic/up.php")
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()
reader = New StreamReader(response.GetResponseStream)
result = reader.ReadToEnd
reader.Close()
Return result
End Function
Finally found the solution -- change the encoding:
Dim encoding As String = "iso-8859-2"
To solve "the response for this request cannot be retrieved until request data has been written" error, you should close the writer
stream before calling request.GetResponse()
, like this:
writer.Write(bytes, 0, bytes.Length)
writer.Close()
response = request.GetResponse()
You don't have to do this in desktop version of .net but you have to in .net compact framework.
I know this is an old question, and my answer is likely be ignored by asker but "the response for this request cannot be retrieved until request data has been written" google search directed me to this question. I hope it helps someone.
精彩评论