>我有两个基本相同的应用程序,它们从文本文件(相同的文件)中读取行,然后将它们发布到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:TestDirTest.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:TestDirTest.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();
}
}
}
我发现问题是由于没有关闭响应。我在循环底部添加了wr.Close()
,它工作没有问题。有趣的是,它不会为 aspx 页超时,但对于 C# 控制台应用程序会超时。