Developing applications that run in the distributed operating environment of today’s Internet requires an efficient, easy-to-use method for retrieving data from resources of all types.
In this article, lets focus on downloading Data from an Internet Server. Next post will talk about uploading the data to the internet server
Client applications that need to make more complex transactions request data from servers using the WebRequest class and its descendants. WebRequest encapsulates the details of connecting to the server, sending the request, and receiving the response.
WebRequest is an abstract class that defines a set of properties and methods that are available to all applications that use pluggable protocols. Descendants of WebRequest, such as HttpWebRequest, implement the properties and methods defined by WebRequest in a way that is consistent with the underlying protocol.
A request is made to the Internet resource by calling the GetResponse method on the WebRequest. The GetResponse method constructs the protocol-specific request from the properties of the WebRequest, makes the TCP or UDP socket connection to the server, and sends the request.
The following code/job example shows how to create a request to a web server and read the data in its response:
class SRWebRequest { public static void main(Args _args) { System.Net.HttpWebResponse response = null; System.Net.WebException webException = null; CLRObject webResponse = null; str errorString; URLBase url = "https://docs.microsoft.com"; const int batchSize = 1024; str responseData; try { System.Net.HttpWebRequest request = null; request = System.Net.WebRequest::Create(url) as System.Net.HttpWebRequest; // If required by the server, set the credentials. //request.Credentials = CredentialCache.DefaultCredentials; webResponse = request.GetResponse(); response = webResponse as System.Net.HttpWebResponse; } catch (Exception::CLRError) { System.Exception exception = ClrInterop::getLastException(); if (exception) { CLRObject clrExceptionMessage = exception.get_message(); errorString = CLRInterop::getAnyTypeForObject(clrExceptionMessage); exception = exception.get_InnerException(); while(exception) { if (exception is System.Net.WebException) { // There is a response body in the answer. This body will be attached in log. webException = exception as System.Net.WebException; webResponse = webException.get_Response(); response = webResponse as System.Net.HttpWebResponse; } clrExceptionMessage = exception.get_Message(); errorString += ‘\n’ + CLRInterop::getAnyTypeForObject(clrExceptionMessage); exception = exception.get_InnerException(); } } } if (response) { System.IO.Stream receiveStream = response.GetResponseStream(); str contentEncoding = response.get_ContentEncoding(); System.Text.Encoding encode; if (contentEncoding) { encode = System.Text.Encoding::GetEncoding(contentEncoding); } else { encode = new System.Text.UTF8Encoding(); } System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, encode); System.Char[] read = new System.Char[batchSize](); int countRead = readStream.Read(read, 0, batchSize); System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.String readString; while (countRead > 0) { readString = new System.String(read, 0, countRead); sb.Append(readString); countRead = readStream.Read(read, 0, batchSize); } readStream.Close(); responseData = sb.ToString(); response.Close(); info(responseData); } } }
Comments
Post a Comment