TwelvestoneBack End

PHP curl vs ASP/ASP.net request - differences?


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
scudsucker
 
2011-12-01

A complete beginner developer has asked me to help him out with something.

Basically he needs to send a specific format XML string to a 3rd party URL; that URL returns an XML response.

He uses (alas) ASP Vbscript, and each time he sends the data, he gets a response saying the XML is missing or malformed.

I fired up a simple C#.NET console app, which does the same thing, and I also get a response saying the XML is missing or malformed.

I can't vouch for his dodgy cut'n'paste style VBscript, but I know that my C# is correct.

I have recorded the POST in Fiddler and ascertained that the XML structure is correct.

The 3rd party API suggests using the following PHP function to send the request:

function call_webservice($url,$xml,&$ret_xml)

  {

    $xml = urlencode($xml);
    $ch = curl_init($url);


    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, "data=".$xml);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT RETURN HTTP HEADERS

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // RETURN THE CONTENTS OF THE CALL

    $returnXML = curl_exec($ch);

    curl_close($ch);


    $ret_xml = new simpleXmlElement($returnXML);

    return true;

  }

My console app uses the following C# function to send the request: private static string CallWebservice(string url, string inputXml) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "text/html";

        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;

        byte[] postBytes = Encoding.ASCII.GetBytes(inputXml);
        request.ContentLength = postBytes.Length;
        request.KeepAlive = true;

        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(postBytes, 0, postBytes.Length);
        }

        HttpWebResponse rsp = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(rsp.GetResponseStream());

        return reader.ReadToEnd();
    }

}

Surely there is no real difference between the two? Both should end up with a request like this (details altered slightly):

POST http://www.domain.com/folder/folder/webentry.cgi HTTP/1.0
Content-Type: text/xml
Host: www.domain.com
Content-Length: 239
Connection: Keep-Alive

<?xml version='1.0'?><webentry><action>addEntry</action><serviceHash>ABCDEFG12345</serviceHash><details><Name>Name</Name><Surname>Surname</Surname><Mobile>123456789</Mobile><UniqueCode>1234</UniqueCode><Age>20</Age></details></webentry>

Am I missing something?

I have tried setting content type to text/plain, text/html, text/xml

I have tried URL encoding the XML before passing it to the function.

I have tested and retested to ensure the XML is valid, and the XML I am using is a direct copy from the 3rd party API

The only thing I can think of, is that somewhere in the PHP function something else is happening - I have never used curl.

scudsucker
 
2011-12-01

OK.. using WebClient I got it working - seems that the XML is sent as a string variable named "data"

Now to try to translate this into VBscript...

    private static string CallWebservice2(string url, string inputXml)
    {
        string URLAuth = url;
        WebClient webClient = new WebClient();

        NameValueCollection formData = new NameValueCollection();
        formData["data"] = inputXml;

        byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
        string response = Encoding.UTF8.GetString(responseBytes);
        webClient.Dispose();

        return response;
    }

}
poliguin
 
2011-12-02

So is he trying to mimic what the web service is doing that is calling to provide a response?

And is there a particular reason to us VB Script? If he's just starting, why not start with something a little more recent.

If you have those actual objects, essentially you have to utilize CreateObject("Object.Class"), for example doing an XML request / response, you would have:

Set XMLHTTP = CreateObject("MSXML2.XMLHTTP.4.0")
Set XMLDoc = CreateObject("MSXML2.DOMDocument")

It essentially like ASP / VB.

Function foo()
    dosomething
end function

Has a return

Sub foo()
    dosomething
end Sub

has no return.

And be wary, declaration is not required (much like Javascript, but at least javascript attempts to validate scope chain), there are no literal types and it is case insensitive.

A simple tutorial on calling a web service: http://stackoverflow.com/questions/545790/how-to-call-web-service-using-vbscript-synchronous

scudsucker
 
2011-12-02

Thanks - not my problem any more, luckily.

He uses VBscript because that is all he knows. He's a designer who has picked up a dangerous amount (ie very little) of coding know-how.

In the end, the problem was not really the code - more the setup of the third party "webservice"

In fact, it is not a webservice in the sense that I as a .NET developer expect: ie, "post XML, receive XML"

This is actually "post a string of xml in a var named 'data' and get back pure XML".. a bit wonky IMHO.

poliguin
 
2011-12-02

Interesting. Much like sending push notification data as well as dealing with receiving it on device.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneBack End

PHP curl vs ASP/ASP.net request - differences?