各位好
我目前在撰寫一個c#的排程
該排程會去發送post api
我在我本機window 10執行,api終端也是window 10
可以順利將所有post執行完成
我在伺服器ibm上執行,api終端是window 10
執行結果只會成功前2筆,第3筆之後就會timeout失敗
我聽我學長建議加間隔,避免太過密集的發post出去
於是我程式中間有加sleep 2秒,結果仍是相同
以下是我的程式碼與結果
還請版上的專家們幫我看看是什麼問題
(懷疑是linux的什麼設定所導致?但我不懂linux)
Dictionary<string, string>[] API_MAP = Enumerable.Range(0, 1).Select(_ => new Dictionary<string, string> {
{"uniform_no1", "https://url/api/receipt.php"},
{"uniform_no2", "https://url/api/receipt.php"},
{"uniform_no3", "https://url/api/receipt.php"},
{"uniform_no4", "https://url/api/receipt.php"},
{"uniform_no5", "https://url/api/receipt.php"}
}).ToArray();
foreach (System.IO.FileInfo file in files)
{
XmlDocument xdoc = new XmlDocument();
XmlNode xRoot;
XmlNodeList xInfoList;
XmlNodeList xDetailList;
xdoc.Load(file.FullName);
xRoot = xdoc.DocumentElement;
xInfoList = xRoot.ChildNodes;
System.Data.DataTable dt = new System.Data.DataTable();
xDetailList = xInfoList.Item(xInfoList.Count - 1).ChildNodes;
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Data.DataTable userDt = DBModule.SelectSQL(String.Format("SELECT * FROM table_users WHERE state = 1 AND uniform_number = '{0}'", dt.Rows[i]["ban"].ToString()));
if (userDt.Rows.Count == 0)
{
foreach (Dictionary<string, string> dc in API_MAP)
{
if (dc.Count == 0) continue;
foreach (KeyValuePair<string, string> kvp in dc)
{
if (dt.Rows[i]["ban"].ToString() == kvp.Key)
{
StringBuilder postDataSb = new StringBuilder();
postDataSb.Append("act=").Append("act");
postDataSb.Append("&uniform_no=").Append(kvp.Key);
postDataSb.Append("&year_month=").Append(dt.Rows[i]["yearmonth"].ToString());
postDataSb.Append("&track=").Append(dt.Rows[i]["invoicetrack"].ToString());
postDataSb.Append("&begin_no=").Append(dt.Rows[i]["invoicebeginno"].ToString());
postDataSb.Append("&end_no=").Append(dt.Rows[i]["invoiceendno"].ToString());
string resultString = APIModule.httpPost(kvp.Value, postDataSb.ToString());
Console.WriteLine("API POST URL:" + kvp.Value);
Console.WriteLine("API POST DATA:" + postDataSb.ToString());
Console.WriteLine("API POST 結果:" + resultString);
Thread.Sleep(2000);
if (resultString == "Ok")
{
Console.WriteLine("[" + DateTime.Now.ToString() + "] " + dt.Rows[i]["ban"].ToString() +
" 透過API新增發票本[" + dt.Rows[i]["invoicebeginno"].ToString() + " ~ " + dt.Rows[i]["invoiceendno"].ToString() + "]成功");
}
else
{
Console.WriteLine("[" + DateTime.Now.ToString() + "] " + dt.Rows[i]["ban"].ToString() +
" 透過API新增發票本[" + dt.Rows[i]["invoicebeginno"].ToString() + " ~ " + dt.Rows[i]["invoiceendno"].ToString() + "]失敗");
}
}
}
}
}
}
}
public static string httpPost(String url, String postData, String auth = "")
{
string result = "";
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (auth != "")
{
request.Headers.Add("Authorization", auth);
}
byte[] bs = System.Text.Encoding.UTF8.GetBytes(postData);
request.ContentLength = bs.Length;
request.GetRequestStream().Write(bs, 0, bs.Length);
//取得 WebResponse 的物件 然後把回傳的資料讀出
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
result = sr.ReadToEnd();
}
catch (Exception ex)
{
return ex.ToString();
}
return result;
}