我有一个通过POST发送XML的代码。但是这段代码是用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);
答案 0 :(得分:1)
您需要使用HttpWebRequest和HttpWebResponse类。代码可能看起来像这样(我的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()
答案 1 :(得分:0)
请参阅:htmlentities solution和urlencode solution
就curl而言,看起来你正试图调用一个Web服务。如果它是一个合适的Web服务(意味着某处有WSDL和XSD),您应该在项目中添加服务引用(或者如果您在VS2005或VS2003中的Web引用),这将为您生成代理使用(而不是手动将XML转储到服务器)。