Files
kami_itunes_june/AppleBatch_June/ApiNetReq.cs

106 lines
2.2 KiB
C#

using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;
using AppleBatch_June.Properties;
using DotNet.Utilities;
using Newtonsoft.Json;
namespace AppleBatch_June
{
public class ApiNetReq
{
private static readonly object reqObj = new object();
public static string host { get; set; } = "";
private static int reqCount { get; set; } = new Random().Next(0, 90);
public static int startNowFun { get; set; } = 0;
public void Transpond(HttpItem httpItem)
{
httpItem.Header = null;
httpItem.ProtocolVersion = null;
httpItem.WebProxy = null;
string postdata = Tools.Toenjson(httpItem);
HttpItem item = new HttpItem
{
URL = "http://localhost:5000/home/Transpond",
Method = "post",
Postdata = postdata,
UserAgent = "liuyeu_AppleBatch_June",
ContentType = "application/json",
Timeout = 35000,
KeepAlive = true
};
new HttpHelper().GetHtml(item);
}
public HttpResult doPost(object postData, string type, bool retry = false)
{
using HttpHelper httpHelper = new HttpHelper();
Stopwatch stopwatch = Stopwatch.StartNew();
var requestData = new
{
data = postData,
retries = 3,
start_now_fun = startNowFun.ToString(),
type_ = type
};
string jsonPostData = JsonConvert.SerializeObject(requestData);
HttpItem httpItem = new HttpItem
{
URL = "http://192.168.31.94:8000/api/apple/june-post",
Method = "post",
Postdata = jsonPostData,
ContentType = "application/json",
Timeout = 35000,
KeepAlive = true
};
HttpResult html = httpHelper.GetHtml(httpItem);
stopwatch.Stop();
if (html.StatusCode != HttpStatusCode.OK && !retry)
{
return doPost(postData, type, retry: true);
}
try
{
if (!string.IsNullOrEmpty(html.Html))
{
var jsonResponse = JsonConvert.DeserializeObject<dynamic>(html.Html);
if (jsonResponse?.data != null)
{
html.Html = jsonResponse.data.ToString();
}
}
}
catch (JsonException)
{
}
APIUtlis.AppRequestHtml(html.Html);
APIUtlis.requestUrl = "ApiNetReq_" + type;
if (httpItem != null)
{
httpItem.Postdata = string.Empty;
}
return html;
}
}
}