Search code examples
c#.netconsole-applicationxml-rpc

Need to read a stream from a URL, changing a "page" attribute in URI until it returns "No records found" string


I have a C# .NET Core console app I'm working on to fetch data from a URL-RPC gateway. I can retrieve the data and write it to a file just fine--just having some trouble even getting started on the logic for incrementing the "page number" attribute in the URI I'm calling against until it returns no more data and the string "No records found." Each "page" of data is about 200 records, so I'll need to increment the page number in the URL until it returns that string.

Here's my basic code (this includes a console write line to verify for debugging purposes, I have other methods to write the data to a file later on.)

string rpcURL;
rpcURL = "https://api.myWebsite.com/urlrpc?method=getPlacementReport&username=" + userName + "&password=" + passWord + "&class_code=" + classCode + "&from_date=" + startDate + "&to_date=" + endDate + "&page_num=1";

Console.WriteLine(rpcURL);

WebClient client = new WebClient();
client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)";
var stream = client.OpenRead(rpcURL);
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();

Console.WriteLine(s);

I know I need to create a variable to increment the end "page_num=NUMBER" part--but I need to increment that by +1 UNTIL the stream reader reads "No records found" EXACTLY.

Any suggestions on an elegant way to do this? I know that basically I'll need to do an if/then statement with an increment +1 counter, but that's about all.


Solution

  • You should be able to accomplish this with a simple while loop. Assuming the StreamReader is expected to return the exact string No records found, you could use something similar to the following.

    WebClient client = new WebClient();
    client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)";
    
    string rpcURL;
    string s = '';
    int page = 0;
    while (s != 'No records found') {
        rpcURL = "https://api.myWebsite.com/urlrpc?method=getPlacementReport&username=" + userName + "&password=" + passWord + "&class_code=" + classCode + "&from_date=" + startDate + "&to_date=" + endDate + "&page_num=" + page;
        Console.WriteLine(rpcURL);
        using(var stream = client.OpenRead(rpcURL)) // both Stream and StreamReader
        using(var sr = new StreamReader(stream))    // need to be disposed.
        {
            s = sr.ReadToEnd();
    
            Console.WriteLine(s);
        } 
        page++;
    }