1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using BHJD.DBdll.Public;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- public class HttpHelper : IHttpHelper
- {
- public Action<string> m_OnShowRequestAddr = null;
- public override string Request(HttpCmd type, object param)
- {
- HttpCmd cmd = type;
- switch (cmd.m_RequestType)
- {
- case HttpRequestType.GET:
- return HttpGet(type, param as List<string>);
- case HttpRequestType.POST:
- return HttpPost(type, param as string);
- default:
- return string.Empty;
- }
- }
- public override string HttpGet(HttpCmd type, List<string> paramList)
- {
- string url = type.m_Addr + "?";
- var kVar = type.m_Args.GetEnumerator();
- int index = 0;
- while (kVar.MoveNext())
- {
- url += kVar.Current + "=" + paramList[index];
- if (index < type.m_Args.Count - 1)
- {
- url += "&";
- }
- index++;
- }
- if (m_OnShowRequestAddr != null)
- {
- m_OnShowRequestAddr(url);
- }
- //Debug.Log(url);
- var request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "GET";
- request.ContentType = "application/json;charset=UTF-8";//ContentType
- var response = (HttpWebResponse)request.GetResponse();
- var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
- return responseString.ToString();
- }
- public override string HttpPost(HttpCmd type, string jsonStr)
- {
- string url = type.m_Addr;
- var request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "POST";
- request.ContentType = "application/json;charset=UTF-8";//ContentType
- byte[] byteData = Encoding.UTF8.GetBytes(jsonStr);
- int length = byteData.Length;
- request.ContentLength = length;
- if (m_OnShowRequestAddr != null)
- {
- m_OnShowRequestAddr(url);
- }
- Stream writer = request.GetRequestStream();
- writer.Write(byteData, 0, length);
- writer.Close();
- var response = (HttpWebResponse)request.GetResponse();
- var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
- return responseString.ToString();
- }
- }
|