304 lines
11 KiB
C#
304 lines
11 KiB
C#
using GameLogic;
|
||
using System;
|
||
using System.IO;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
|
||
public static class HttpSendHelper
|
||
{
|
||
|
||
/// <summary>
|
||
/// Http Get Request
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <returns></returns>
|
||
public static string HttpGetRequest(string url)
|
||
{
|
||
string strGetResponse = string.Empty;
|
||
try
|
||
{
|
||
var getRequest = CreateHttpRequest(url, "GET");
|
||
var getResponse = getRequest.GetResponse() as HttpWebResponse;
|
||
strGetResponse = GetHttpResponse(getResponse, "GET");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
strGetResponse = ex.Message;
|
||
}
|
||
return strGetResponse;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Http Get Request Async
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
public static async Task<string> HttpGetRequestAsync(string url)
|
||
{
|
||
string strGetResponse = string.Empty;
|
||
try
|
||
{
|
||
var getRequest = CreateHttpRequest(url, "GET");
|
||
var getResponse = await getRequest.GetResponseAsync() as HttpWebResponse;
|
||
strGetResponse = GetHttpResponse(getResponse, "GET");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
strGetResponse = ex.Message;
|
||
}
|
||
|
||
return strGetResponse;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Http Post Request
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="postJsonData"></param>
|
||
/// <returns></returns>
|
||
public static string HttpPostRequest(string url, string postJsonData)
|
||
{
|
||
string strPostReponse = string.Empty;
|
||
try
|
||
{
|
||
var postRequest = CreateHttpRequest(url, "POST", postJsonData);
|
||
var postResponse = postRequest.GetResponse() as HttpWebResponse;
|
||
strPostReponse = GetHttpResponse(postResponse, "POST");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
strPostReponse = ex.Message;
|
||
}
|
||
return strPostReponse;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Http Post Request Async
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="postJsonData"></param>
|
||
public static async Task<string> HttpPostRequestAsync(string url, string postData)
|
||
{
|
||
//UnityEngine.Debug.LogError($"{url} {postData}");
|
||
string strPostReponse = string.Empty;
|
||
try
|
||
{
|
||
var postRequest = CreateHttpRequest(url, "POST", postData);
|
||
var postResponse = await postRequest.GetResponseAsync() as HttpWebResponse;
|
||
strPostReponse = GetHttpResponse(postResponse, "POST");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
strPostReponse = ex.Message;
|
||
}
|
||
if (strPostReponse != "true")
|
||
{
|
||
Console.WriteLine("--> reslut : " + strPostReponse);
|
||
Console.WriteLine(postData);
|
||
}
|
||
|
||
return strPostReponse;
|
||
}
|
||
static int x = 1;
|
||
/// <summary>
|
||
/// Http Post Request Async
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="postJsonData"></param>
|
||
public static async Task<string> HttpPostRequest_Async(string url, string postData)
|
||
{
|
||
|
||
//UnityEngine.Debug.LogError($"{url} {postData}");
|
||
x += 10;
|
||
await Task.Delay(x);
|
||
if (x == 60)
|
||
{
|
||
x = 1;
|
||
}
|
||
//string strPostReponse = string.Empty;
|
||
return await Task.Run<string>(async () =>
|
||
{
|
||
string strPostReponse = string.Empty;
|
||
var postRequest = HttpWebRequest.Create(url) as HttpWebRequest;
|
||
|
||
try
|
||
{
|
||
postRequest.KeepAlive = false;
|
||
postRequest.Timeout = 2000;
|
||
postRequest.ReadWriteTimeout = 2000;
|
||
postRequest.Method = "POST";
|
||
postRequest.ContentType = "application/json";
|
||
postRequest.ContentLength = Encoding.UTF8.GetByteCount(postData);
|
||
postRequest.Headers.Add("appId", EventConts.PlatformType == PlatformType.None ? EventConts.app_id_test : EventConts.app_id);
|
||
postRequest.AllowWriteStreamBuffering = true;
|
||
|
||
using (StreamWriter writer = new StreamWriter(postRequest.GetRequestStream(), new UTF8Encoding(false)))
|
||
{
|
||
await writer.WriteAsync(postData);
|
||
await writer.FlushAsync();
|
||
var response = await postRequest.GetResponseAsync();
|
||
using (Stream responseStream = response.GetResponseStream())
|
||
{
|
||
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
|
||
{
|
||
// <20>첽<EFBFBD><ECB2BD>ȡȫ<C8A1><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
strPostReponse = await reader.ReadToEndAsync();
|
||
}
|
||
}
|
||
}
|
||
|
||
return strPostReponse;
|
||
//postRequest.Abort();
|
||
//postRequest.l
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UnityEngine.Debug.LogError($"HttpPostRequest_Async:{ex.Message}");
|
||
return "";
|
||
}
|
||
});
|
||
//return "";
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// Http Post Request Async
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="postJsonData"></param>
|
||
public static async Task<string> HttpPostRequest_Async2(string url, string postData)
|
||
{
|
||
//string strPostReponse = string.Empty;
|
||
return await Task.Run<string>(async () =>
|
||
{
|
||
string strPostReponse = string.Empty;
|
||
var postRequest = HttpWebRequest.Create(url) as HttpWebRequest;
|
||
|
||
try
|
||
{
|
||
postRequest.KeepAlive = false;
|
||
postRequest.Timeout = 2000;
|
||
postRequest.ReadWriteTimeout = 2000;
|
||
postRequest.Method = "POST";
|
||
postRequest.ContentType = "application/json";
|
||
postRequest.ContentLength = Encoding.UTF8.GetByteCount(postData);
|
||
postRequest.Headers.Add("appId", EventConts.PlatformType == PlatformType.None? EventConts.app_id_test : EventConts.app_id);
|
||
postRequest.AllowWriteStreamBuffering = true;
|
||
|
||
using (StreamWriter writer = new StreamWriter(postRequest.GetRequestStream(), new UTF8Encoding(false)))
|
||
{
|
||
await writer.WriteAsync(postData);
|
||
await writer.FlushAsync();
|
||
var response = await postRequest.GetResponseAsync();
|
||
using (Stream responseStream = response.GetResponseStream())
|
||
{
|
||
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
|
||
{
|
||
// <20>첽<EFBFBD><ECB2BD>ȡȫ<C8A1><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
strPostReponse = await reader.ReadToEndAsync();
|
||
}
|
||
}
|
||
}
|
||
|
||
return strPostReponse;
|
||
//postRequest.Abort();
|
||
//postRequest.l
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UnityEngine.Debug.LogError($"HttpPostRequest_Async:{ex.Message}");
|
||
return "";
|
||
}
|
||
});
|
||
//return "";
|
||
}
|
||
|
||
|
||
private static HttpWebRequest CreateHttpRequest(string url, string requestType, params object[] strJson)
|
||
{
|
||
HttpWebRequest request = null;
|
||
const string get = "GET";
|
||
const string post = "POST";
|
||
if (string.Equals(requestType, get, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
request = CreateGetHttpWebRequest(url);
|
||
}
|
||
if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
request = CreatePostHttpWebRequest(url, strJson[0].ToString());
|
||
}
|
||
return request;
|
||
}
|
||
|
||
private static HttpWebRequest CreateGetHttpWebRequest(string url)
|
||
{
|
||
var getRequest = HttpWebRequest.Create(url) as HttpWebRequest;
|
||
getRequest.Method = "GET";
|
||
getRequest.Timeout = 20000;
|
||
getRequest.ReadWriteTimeout = 32000;
|
||
getRequest.ContentType = "text/html;charset=UTF-8";
|
||
getRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
|
||
getRequest.Headers.Add("appId", EventConts.PlatformType == PlatformType.None ? EventConts.app_id_test : EventConts.app_id);
|
||
return getRequest;
|
||
}
|
||
|
||
private static HttpWebRequest CreatePostHttpWebRequest(string url, string postData)
|
||
{
|
||
var postRequest = HttpWebRequest.Create(url) as HttpWebRequest;
|
||
postRequest.KeepAlive = false;
|
||
postRequest.Timeout = 20000;
|
||
postRequest.ReadWriteTimeout = 32000;
|
||
postRequest.Method = "POST";
|
||
postRequest.ContentType = "application/json";
|
||
postRequest.ContentLength = Encoding.UTF8.GetByteCount(postData);
|
||
postRequest.Headers.Add("appId", EventConts.PlatformType == PlatformType.None ? EventConts.app_id_test : EventConts.app_id);
|
||
postRequest.AllowWriteStreamBuffering = false;
|
||
StreamWriter writer = new StreamWriter(postRequest.GetRequestStream(), new UTF8Encoding(false));
|
||
writer.Write(postData);
|
||
writer.Flush();
|
||
return postRequest;
|
||
}
|
||
|
||
private static string GetHttpResponse(HttpWebResponse response, string requestType)
|
||
{
|
||
var responseResult = "";
|
||
const string post = "POST";
|
||
string encoding = "UTF-8";
|
||
if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
encoding = response.ContentEncoding;
|
||
if (encoding == null || encoding.Length < 1)
|
||
{
|
||
encoding = "UTF-8";
|
||
}
|
||
}
|
||
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
|
||
{
|
||
//responseResult = await reader.ReadToEndAsync();
|
||
responseResult = reader.ReadToEnd();
|
||
}
|
||
return responseResult;
|
||
}
|
||
|
||
private static async Task<string> GetHttpResponseAsync(HttpWebResponse response, string requestType)
|
||
{
|
||
var responseResult = "";
|
||
const string post = "POST";
|
||
string encoding = "UTF-8";
|
||
if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
encoding = response.ContentEncoding;
|
||
if (encoding == null || encoding.Length < 1)
|
||
{
|
||
encoding = "UTF-8";
|
||
}
|
||
}
|
||
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
|
||
{
|
||
responseResult = await reader.ReadToEndAsync();
|
||
}
|
||
return responseResult;
|
||
}
|
||
}
|