2009-12-02 175 views
0

我有一個通過POST發送XML的代碼。但是這段代碼是用PHP編寫的,我在VB.NET中需要它。幫助將此PHP代碼轉換爲VB.NET代碼

將此代碼轉換的任何幫助?

$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"; 

和我如何轉換這個PHP代碼太:

$msg=htmlentities($msg); 
$msg=urlencode($msg); 

回答

1

您需要使用HttpWebRequestHttpWebResponse類。代碼可能看起來像這樣(我的VB有點生鏽這幾天):

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() 
0

見:htmlentities solutionurlencode solution

並儘可能捲曲,它看起來像你試圖調用Web服務。如果它是一個合適的Web服務(這意味着有一個WSDL和一個XSD),您應該向您的項目添加一個服務引用(或者Web引用,如果您在VS2005或VS2003中),它將爲您生成一個代理使用(而不是手動將XML轉儲到服務器)。