0
我有兩個基本相同的應用程序,它們從文本文件(同一個)讀取行並將它們發佈到URL。當我在.aspx頁面中運行代碼時,代碼始終沒有問題。當我在控制檯應用程序中運行它時,它只會成功發佈文本文件的前兩行,然後在前兩行後總是超時。這對我來說沒有任何意義,因爲我得到了前兩行的成功響應,然後它始終在同一點上失敗,但對於aspx頁面,我得到所有行的響應(例如10行)。我希望在控制檯應用程序表單中這樣我可以安排它使用Windows任務計劃程序定期運行。發佈將C#控制檯應用程序超時但不是aspx頁面
我的代碼有什麼問題,或者它與使用控制檯應用程序有關嗎?
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();
}
}
}