【问题标题】:Post will timeout for C# Console Application but not aspx pagePost 将超时 C# 控制台应用程序但不是 aspx 页面
【发布时间】:2014-02-21 22:16:43
【问题描述】:

我有两个基本相同的应用程序,它们从文本文件(同一个)中读取行,然后将它们发布到 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# 控制台应用:

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

【问题讨论】:

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


    【解决方案1】:

    我发现问题是由于没有关闭响应。我在循环底部添加了wr.Close(),它可以正常工作。有趣的是,aspx 页面不会超时,但 C# 控制台应用程序会超时。

    【讨论】:

      猜你喜欢
      • 2011-04-28
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多