Boletín mugperu Digital - Julio 2009!
  Search 
Thursday, February 09, 2012 ..:: Foros de Discusión ::.. Register  Login
Foros MUGPERU Minimize
Subject: HttpWebRequest para enviar POST y recibir XML
Prev Next
You are not authorized to post a reply.

Author Messages
Kurono
Posts:0

02/05/2007 08:24 AM  
Necesito realizar una funcion que sea capaz de enviar un parametro mediante el metodo "POST" a una web y poder trastear con la respuesta para interpretar si el resultado ha sido el esperado o no.
 
Es para un servidor de sms, yo le envio un xml mediante el post y el me responde con un codigo xml con el resultado.
 
He realizado una función que utiliza el httpwebrequest y consigo realizar el post sin problemas, pero al intentar acceder a la respuesta utilizando el metodo .getresponse() se produce una excepción "WebException" cuyo status es ServerProtocolViolation.
 
Gracias por la ayuda

PD: La pagina que devuelve el xml es esta:

http://sms.lleida.net/xmlapi/smsgw.cgi

PD2: La funcion que tengo hecha y produce una excepcion al hacer el .getresponse() es esta (url es la direccion y payload el parametro del post) :
 
------------------------------------------------------------------------------------------

Public Sub getPage(ByVal url As String, ByVal payload As String)

Dim result As Object

Try

Dim req As HttpWebRequest

Dim RequestStream As Stream

Dim ReceiveStream As Stream

Dim encode As Encoding

Dim sr As StreamReader

Dim alg As Xml.XmlTextReader

req = HttpWebRequest.Create(url)

req.Method = "POST"

req.ContentType = "application/x-www-form-urlencoded"

Dim SomeBytes() As Byte

Dim UrlEncoded As New StringBuilder

Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}

req.PreAuthenticate = False

req.UnsafeAuthenticatedConnectionSharing = True

req.AllowAutoRedirect = True

 

If payload <> Nothing Then

Dim i As Integer = 0

Dim j As Integer

While i < payload.Length

j = payload.IndexOfAny(reserved, i)

If j = -1 Then

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length - i)))

Exit While

End If

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j - i)))

UrlEncoded.Append(payload.Substring(j, 1))

i = j + 1

End While

SomeBytes = System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString())

req.ContentLength = SomeBytes.Length

RequestStream = req.GetRequestStream()

RequestStream.Write(SomeBytes, 0, SomeBytes.Length)

RequestStream.Close()

Else

req.ContentLength = 0

End If

'Leyendo respuesta de la web

result = req.GetResponse()

ReceiveStream = result.GetResponseStream()

encode = System.Text.Encoding.GetEncoding("utf-8")

sr = New StreamReader(ReceiveStream, encode)

 

Dim read(256) As Char

Dim count As Integer = sr.Read(read, 0, 256)

 

Do While count > 0

Dim str As String = New String(read, 0, count)

Literal1.Text += str

count = sr.Read(read, 0, 256)

Loop

Catch Exc As WebException

Literal1.Text = Exc.Response.ContentLength

Finally

If Not result Is Nothing Then

result.Close()

End If

End Try

End Sub

freich
Posts:1

20/01/2010 01:59 PM  
Buenos días

La consulta que voy a realizar no estoy seguro si tiene algo que ver con el tema del foro pero si me pueden ayudar, les estaré muy agradecido.

Por favor ayúdenme, con esto:

Necesito crear un script que envíe parámetros post a una web.

La web es “http://evtde.argonautanet.com/Varios/ComprobacionTelefono.aspx”.

Y los parámetros post que quiero enviar son:

FRAME F=0
TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:aspnetForm ATTR=ID:ctl00_Main_txtTelefono CONTENT=12344556
ONDIALOG POS=1 BUTTON=OK CONTENT=
TAG POS=1 TYPE=INPUT:IMAGE FORM=ID:aspnetForm ATTR=ID:ctl00_Main_BtnComprobar

Saludos
You are not authorized to post a reply.
Forums > Temas de Interés > Construcción de Aplicaciones Web con ASP.NET > HttpWebRequest para enviar POST y recibir XML



ActiveForums 3.7
        
Copyright 2001-2012 MUGPERU   Terms Of Use  Privacy Statement