Ver Mensaje Individual
  #4 (permalink)  
Antiguo 12/10/2009, 08:54
Avatar de seinkraft
seinkraft
 
Fecha de Ingreso: diciembre-2007
Mensajes: 119
Antigüedad: 16 años, 4 meses
Puntos: 1
Respuesta: POST en vb.net

Bueno por el momento logre esto:

Código vb:
Ver original
  1. Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
  2.         Dim request As HttpWebRequest
  3.         Dim response As HttpWebResponse = Nothing
  4.         Dim reader As StreamReader
  5.         Dim address As Uri
  6.         Dim data As StringBuilder
  7.         Dim byteData() As Byte
  8.         Dim postStream As Stream = Nothing
  9.  
  10.         Dim username As String
  11.         Dim password As String
  12.         Dim file_location As String
  13.         Dim tags As String
  14.         Dim source As String
  15.         Dim md5 As String
  16.  
  17.         address = New Uri("http://localhost/api/add_image")
  18.  
  19.         ' Create the web request  
  20.        request = DirectCast(WebRequest.Create(address), HttpWebRequest)
  21.  
  22.         ' Set type to POST  
  23.        request.Method = "POST"
  24.         request.ContentType = "multipart/form-data"
  25.  
  26.         ' Create the data we want to send
  27.        username = "user"
  28.         password = "userpass"
  29.         file_location = txtFile.Text
  30.         tags = "coco nu_se"
  31.         source = "http://www.seinkraft.info"
  32.         md5 = "2ef87d61eb6c5d318760db51828d8d00"
  33.  
  34.         data = New StringBuilder()
  35.         data.Append("login=" + username)
  36.         data.Append("&password=" + password)
  37.         data.Append("&file=" + file_location)
  38.         data.Append("&md5=" + md5)
  39.         data.Append("&tags=" + tags)
  40.         data.Append("&source=" + source)
  41.  
  42.         TextBox1.Text = data.ToString
  43.  
  44.         ' Create a byte array of the data we want to send  
  45.        byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
  46.  
  47.         ' Set the content length in the request headers  
  48.        request.ContentLength = byteData.Length
  49.  
  50.         ' Write data  
  51.        Try
  52.             postStream = request.GetRequestStream()
  53.             postStream.Write(byteData, 0, byteData.Length)
  54.         Finally
  55.             If Not postStream Is Nothing Then postStream.Close()
  56.         End Try
  57.  
  58.         Try
  59.             ' Get response  
  60.            response = DirectCast(request.GetResponse(), HttpWebResponse)
  61.  
  62.             ' Get the response stream into a reader  
  63.            reader = New StreamReader(response.GetResponseStream())
  64.  
  65.             ' Console application output  
  66.            TextBox2.Text = reader.ReadToEnd()
  67.         Finally
  68.             If Not response Is Nothing Then response.Close()
  69.         End Try
  70.     End Sub

Y los parametros de la api son:
Parameters
* login: login
* 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

Lo que no se es como hacer la parte del file (ahora esta echa asi nomas). No se como enviarlo como multipart

Alguien me puede ayudar con ello?

Última edición por seinkraft; 12/10/2009 a las 09:02