Introduction
This is a Basic introduction for posting an HTTP/S request to an external system using the NetObj
interface to the DotNet framework. While this request is not asynchronous (it might block CET while running) it should solve the purpose for posting the request to dot net.
NetObj host(); try { //Use the DotNet WebRequest for Actual Posting NetObj webReqClass = host.createType("System.Net.WebRequest"); if (!webReqClass) { return null; } //Equalent to WebRequest webReq = WebRequest.Creat("http://tars.greycode.in") NetObj webReq = webReqClass.callObj("Create", url); if (!webReq) { return null; } //Equivalent to webReq.Method = "POST"; webReq.set("Method","POST"); webReq.set("ContentType","application/text;charset=UTF-8"); //Get the Encoded UTF-8 Byte Array using DotNet NetObj encodingType = host.createType("System.Text.Encoding"); if (!encodingType) { return null; }
NetObj encoding = encodingType.callObj("GetEncoding", "UTF-8"); if (!encoding) { return null; } NetObj requestBytes = encoding.callObj("GetBytes", content); if (!requestBytes) { return null; } int requestLength = requestBytes.getInt("Length");
webReq.set("ContentLength", requestLength); NetObj dataStream = webReq.callObj("GetRequestStream"); if (!dataStream) { return null; } dataStream.callVoid("Write", requestBytes, 0, requestLength); dataStream.callVoid("Close"); NetObj response = webReq.callObj("GetResponse"); if (!response) { return null; } dataStream = response.callObj("GetResponseStream"); if (!dataStream) { return null; } NetObj reader = host.createInstance("System.IO.StreamReader", dataStream); if (reader) { str responseText = reader.callStr("ReadToEnd"); //pln(#responseText); return responseText; } else { ptrace("Reader not Created"); } return null; }finally { host.dispose(); }
Comments
0 comments
Please sign in to leave a comment.