邮件将超时C#控制台应用程序但不是aspx页面

时间:2014-02-21 22:16:44

标签: c# asp.net post timeout console-application

我有两个基本相同的应用程序,它们从文本文件(同一个文件)读取行,然后将它们发布到URL。当我在.aspx页面中运行代码时,代码始终发布没有问题。当我在控制台应用程序中运行它时,它将仅成功发布文本文件的前两行,然后在前两行之后总是超时。这对我来说没有意义,因为我得到前两行的成功响应,然后它总是在同一点失败,但对于aspx页面,我得到所有行的响应(例如10行)。我想在控制台应用程序表单中这样,所以我可以安排它使用Windows任务计划程序定期运行。

我的代码有问题,还是与使用控制台应用程序有关?

ASPX页面:

<%@ 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();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我发现问题是由于没有关闭响应。我将wr.Close()添加到循环的底部,它没有问题。有趣的是,它不会超时用于aspx页面,而是用于C#控制台应用程序。