Question

I've got two basically identical applications which read lines from a text file (the same one) and then post them to a URL. When I run the code in an .aspx page, the code always posts with no issue. When I run it in a console application, it will post successfully for only the first two lines of the text file, and then always times out after the first two lines. It doesn't make sense to me, as I get the success response for the first two lines, and then it always fails at the same point, but for the aspx page, I get responses for all lines (for example 10 lines). I want this in console application form so I can schedule it to run regularly using Windows Task Scheduler.

Is there something wrong with my code, or is it something to do with using a console application?

ASPX page:

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Net"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net.Mail"%>
<%
    //FILE PATH WHICH DATA IS PULLED FROM FOR POST
    string fileName = @"C:\TestDir\Test.txt";

    //READ ALL LINES OF TEXT FILE INTO AN ARRAY
    string[] lines = System.IO.File.ReadAllLines(fileName);
    string url = "http://testurl.com/test";

    //READ TEXT FILE LINE BY LINE
    foreach (string line in lines)
    {
        HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
        HttpWebResponse wr = null;
        string user = "testuser";
        string pwd = "testpassword";
        string mydata = line;
        byte[] byteData = Encoding.UTF8.GetBytes(mydata);
        UTF8Encoding enc = new UTF8Encoding();
        req.Method = "POST";
        string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
        req.PreAuthenticate = true;
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = byteData.Length;
        req.Accept = "application/json";
        req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
        req.Headers.Add("Authorization", auth);
        using (Stream ds = req.GetRequestStream())
        {
            ds.Write(byteData, 0, byteData.Length);
            ds.Close();
        } try
        {
            wr = (HttpWebResponse)req.GetResponse();
        }
        catch (
        WebException ex)
        {
            wr = (HttpWebResponse)ex.Response;
        }
        Stream receiveStream = wr.GetResponseStream();
        StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
        string content = reader.ReadToEnd();
        Response.Write(content + "Success");
    }
 %>

C# console app:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

    namespace testConsoleAPI
    {
        class Program
        {
            static void Main(string[] args)
            {
                //FILE PATH WHICH DATA IS PULLED FROM FOR POST
                string fileName = @"C:\TestDir\Test.txt";

                //READ ALL LINES OF TEXT FILE INTO AN ARRAY
                string[] lines = System.IO.File.ReadAllLines(fileName);
                string url = "http://testurl.com/test";

                //READ TEXT FILE LINE BY LINE
                foreach (string line in lines)
                {
                    HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
                    HttpWebResponse wr = null;
                    string user = "testuser";
                    string pwd = "testpassword";
                    string mydata = line;
                    byte[] byteData = Encoding.UTF8.GetBytes(mydata);
                    UTF8Encoding enc = new UTF8Encoding();
                    req.Method = "POST";
                    string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
                    req.PreAuthenticate = true;
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.ContentLength = byteData.Length;
                    req.Accept = "application/json";
                    req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
                    req.Headers.Add("Authorization", auth);
                    using (Stream ds = req.GetRequestStream())
                    {
                        ds.Write(byteData, 0, byteData.Length);
                        ds.Close();
                    } try
                    {
                        wr = (HttpWebResponse)req.GetResponse();
                    }
                    catch (
                    WebException ex)
                    {
                        wr = (HttpWebResponse)ex.Response;
                    }
                    Stream receiveStream = wr.GetResponseStream();
                    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                    string content = reader.ReadToEnd();
                    Console.WriteLine(content);
                }
                Console.ReadLine();
            }
        }
    }
Was it helpful?

Solution

I found that the issue was due to not closing the response. I added wr.Close() to the bottom of the loop, and it worked without a problem. It's interesting that it would not time out for the aspx page, but would for the C# console application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top