seinkraft
Member
- Joined
- Sep 28, 2008
- Messages
- 19
- Programming Experience
- 5-10
Hi everyone, i'm really new with HttpWebRequest and i don't understand it as well but i need it to use an api from my web to post images.
I've this at the moment.
And the api parameters are:
Parameters
* login: username
* password: password
* file: file as a multipart form
* source: source url
* tags: list of tags as a string, delimited by whitespace
* md5: MD5 hash of upload in hexadecimal format
but i don't know how send the file as multipart.
Anyone can help me?
(sorry about my poor english)
I've this at the moment.
VB.NET:
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim address As Uri
Dim data As StringBuilder
Dim byteData() As Byte
Dim postStream As Stream = Nothing
Dim username As String
Dim password As String
Dim file_location As String
Dim tags As String
Dim source As String
Dim md5 As String
address = New Uri("http://localhost/api/add_image")
' Create the web request
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
' Set type to POST
request.Method = "POST"
request.ContentType = "multipart/form-data"
' Create the data we want to send
username = "sein"
password = "1234"
file_location = txtFile.Text
tags = "coco nu_se"
source = "http://www.seinkraft.info"
md5 = "2ef87d61eb6c5d318760db51828d8d00"
data = New StringBuilder()
data.Append("login=" + username)
data.Append("&password=" + password)
data.Append("&file=" + file_location)
data.Append("&md5=" + md5)
data.Append("&tags=" + tags)
data.Append("&source=" + source)
TextBox1.Text = data.ToString
' Create a byte array of the data we want to send
byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
' Set the content length in the request headers
request.ContentLength = byteData.Length
' Write data
Try
postStream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)
Finally
If Not postStream Is Nothing Then postStream.Close()
End Try
Try
' Get response
response = DirectCast(request.GetResponse(), HttpWebResponse)
' Get the response stream into a reader
reader = New StreamReader(response.GetResponseStream())
' Console application output
TextBox2.Text = reader.ReadToEnd()
Finally
If Not response Is Nothing Then response.Close()
End Try
End Sub
And the api parameters are:
Parameters
* login: username
* password: password
* file: file as a multipart form
* source: source url
* tags: list of tags as a string, delimited by whitespace
* md5: MD5 hash of upload in hexadecimal format
but i don't know how send the file as multipart.
Anyone can help me?
(sorry about my poor english)