public async Task <string> MakePostRequest(string id, string apiName, ProgressMessageHandler progressMessageHandler, bool save = false) { return(await Task.Run(async() => { var makeParams = new MakeParams(); makeParams.AddSignatureParam(id, apiName); HttpContent httpContent = new StringContent(makeParams.GetParam()); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = "utf-8" }; using (var client = new HttpClient(progressMessageHandler) { BaseAddress = new Uri(Define.BaseUrl) }) { client.DefaultRequestHeaders.Add("Expect", "100-continue"); client.DefaultRequestHeaders.Add("X-Unity-Version", "2018.2.6f1"); client.DefaultRequestHeaders.Add("UserAgent", "Dalvik/2.1.0 (Linux; U; Android 5.1.1; xiaomi 8 Build/LMY49I)"); client.DefaultRequestHeaders.Add("Host", "api.t7s.jp"); client.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip"); var httpResponse = client.PostAsync(Define.GetApiName(Define.APINAME_TYPE.result) , httpContent).Result; httpResponse.EnsureSuccessStatusCode(); //ManualResetEvent.WaitOne(100); return await httpResponse.Content.ReadAsStringAsync(); } })); }
/// <summary> /// 生成Post请求 /// </summary> /// <param name="postUrl">POST地址</param> /// <param name="id">用户的id</param> /// <param name="apiName">请求的apiName</param> /// <returns></returns> public async Task <string> RawMakePostRequest(string postUrl, string id, string apiName, bool save = false) { var makeParams = new MakeParams(); makeParams.AddSignatureParam(id, apiName); var PrarmsBytes = Encoding.UTF8.GetBytes(makeParams.GetParam()); var request = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Headers.Add("X-Unity-Version", "2018.2.6f1"); request.ContentLength = PrarmsBytes.Length; request.UserAgent = "Dalvik/2.1.0 (Linux; U; Android 5.1.1; xiaomi 8 Build/LMY49I)"; request.Host = "api.t7s.jp"; request.Headers.Add("Accept-Encoding", "gzip"); //request.Expect = "100-continue"; //request.Connection = "Keep-Alive"; //request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; //if (!request.HaveResponse) //{ // System.Windows.Forms.MessageBox.Show("网络异常,请重试!", "错误", System.Windows.Forms.MessageBoxButtons.RetryCancel); //} using (var requestStream = request.GetRequestStream()) { requestStream.Write(PrarmsBytes, 0, PrarmsBytes.Length); } using (var response = (HttpWebResponse)request.GetResponse()) { using (var streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { var JsonString = await streamReader.ReadToEndAsync(); if (save) { if (!Directory.Exists(Define.LocalPath + @"\Asset\Result")) { Directory.CreateDirectory(Define.LocalPath + @"\Asset\Result"); } using (var streamWriter = new StreamWriter(Define.LocalPath + @"\Asset\Result\" + "Result.json")) { await streamWriter.WriteAsync(JsonString); streamWriter.Close(); } } return(JsonString); } } }
public async Task <string> MakePostRequest(string id, string apiName, bool update = false) { try { var makeParams = new MakeParams(); makeParams.AddParam("userRev", Define.UserRev); makeParams.AddCommonParams(); makeParams.AddParam("pid", SaveData.Decrypt(Define.encPid)); makeParams.AddSignatureParam(id, apiName); var httpContent = new StringContent(makeParams.GetParam()) { Headers = { ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = "utf-8" } } }; var response = await _postClient.PostAsync(Define.GetApiName(Define.APINAME_TYPE.result) , httpContent); if (response.IsSuccessStatusCode) { var jsonString = await response.Content.ReadAsStringAsync(); if (!update) { return(jsonString); } using (var streamWriter = new StreamWriter(Define.GetUpdatePath())) { streamWriter.Write(jsonString); streamWriter.Close(); } } else { response.EnsureSuccessStatusCode(); MessageBox.Show(@"请求超时"); } return("complete"); } catch (Exception e) { MessageBox.Show(e.Message); throw; } }
public async Task <int> MakeUpdatePostRequest(string id, string apiName) { try { var Params = new MakeParams(); Params.AddParam("downloadType", "0"); Params.AddParam("userRev", Define.UserRev); Params.AddCommonParams(); Params.AddParam("pid", SaveData.Decrypt(Define.encPid)); Params.AddSignatureParam(id, apiName); var httpContent = new StringContent(Params.GetParam()) { Headers = { ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") } }; var response = await _postClient.PostAsync(apiName , httpContent); if (response.IsSuccessStatusCode) { var jsonString = await response.Content.ReadAsStringAsync(); using (var streamWriter = new StreamWriter(Define.GetUpdatePath())) { streamWriter.Write(jsonString); streamWriter.Close(); } return(0); } response.EnsureSuccessStatusCode(); return(1); } catch (Exception e) { MessageBox.Show(e.Message); throw; } }