Ayudar a convertir el código PHP para código VB.NET
Pregunta
Tengo un código para enviar XML a través de POST. Pero este es el código de PHP y lo necesito en VB.NET.
Cualquier ayuda para convertir este código?
$XMLFile= (here i have created the xml file. XML is encoded ISO-8859)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"URL WHERE I SEND XML");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"XMLDATA=".$XMLFile);
$results=curl_exec ($ch);
curl_close ($ch);
$results=stripslashes($results);
$xmlreturned=new SimpleXMLElement($results);
if($xmlreturned->NotificationResultHeader->RRC==0){
if($xmlreturned->NotificationResultList->NotificationResult->NRC==0){
echo "OK. SUCCES";
Y cómo puedo convertir el código PHP también:
$msg=htmlentities($msg);
$msg=urlencode($msg);
Solución
Es necesario utilizar el HttpWebRequest y HttpWebResponse clases. El código podría sería algo como esto ( mi VB es un poco oxidado en estos días ):
Dim xmlDoc as XmlDocumnet
'
' prepare you xml doc here...
'
Dim encoding as ASCIIEncoding = New ASCIIEncoding()
Dim postData as String
postData = "XMLDATA=" + xmlDoc.ToString()
Dim data() as Byte
data = encoding.GetBytes(postData)
' Prepare web request...
Dim myRequest as HttpWebRequest
myRequest = CType(WebRequest.Create("URL TO POST HERE"), HttpWebRequest)
myRequest.Method = "POST"
myRequest.ContentType="application/x-www-form-urlencoded"
myRequest.ContentLength = data.Length
Dim newStream as Stream = myRequest.GetRequestStream()
' Send the data.
newStream.Write(data, 0, data.Length)
' Get the response
Dim myResponse as HttpWebResponse
myResponse = myRequest.GetResponse()
Otros consejos
Ver: htmlentities solución urlencode
Y en lo que rizo, parece que usted está tratando de llamar a un servicio web. Si se trata de un servicio web adecuada (es decir, hay un WSDL y un XSD en algún lugar), se debe añadir una referencia de servicio (o una referencia Web si estás en VS2005 o VS2003) a su proyecto, que generará un proxy para que usted pueda utilizar (en lugar de vertido manualmente XML a un servidor).