static protected void waitPostHttpWebRequest(object param) { RequestThreadParam threadParam = param as RequestThreadParam; try { //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) Stream newStream = threadParam.mRequest.GetRequestStream(); //创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 newStream.Write(threadParam.mByteArray, 0, threadParam.mByteArray.Length); newStream.Close(); //4. 读取服务器的返回信息 HttpWebResponse response; response = (HttpWebResponse)threadParam.mRequest.GetResponse(); StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string phpend = php.ReadToEnd(); php.Close(); response.Close(); threadParam.mCallback(JsonMapper.ToObject(phpend), threadParam.mUserData); } catch (Exception) { threadParam.mCallback(null, threadParam.mUserData); } finally { mHttpThreadList.Remove(threadParam.mThread); } }
static protected void waitGetHttpWebRequest(object param) { RequestThreadParam threadParam = param as RequestThreadParam; try { HttpWebResponse response = (HttpWebResponse)threadParam.mRequest.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { UnityUtility.logInfo("接受超时"); } Stream steam = response.GetResponseStream(); StreamReader reader = new StreamReader(steam, Encoding.UTF8); string pageStr = reader.ReadToEnd(); reader.Close(); response.Close(); reader = null; response = null; threadParam.mRequest.Abort(); threadParam.mRequest = null; threadParam.mCallback(JsonMapper.ToObject(pageStr), threadParam.mUserData); } catch (Exception) { threadParam.mCallback(null, threadParam.mUserData); } finally { mHttpThreadList.Remove(threadParam.mThread); } }
static protected void waitGetHttpWebRequest(object param) { RequestThreadParam threadParam = param as RequestThreadParam; try { HttpWebResponse response = (HttpWebResponse)threadParam.mRequest.GetResponse(); Stream steam = response.GetResponseStream(); StreamReader reader = new StreamReader(steam, Encoding.UTF8); string pageStr = reader.ReadToEnd(); reader.Close(); response.Close(); reader = null; response = null; threadParam.mRequest.Abort(); threadParam.mRequest = null; threadParam.mCallback(JsonMapper.ToObject(pageStr), threadParam.mUserData); } catch (Exception e) { threadParam.mCallback(null, threadParam.mUserData); string info = "http get result exception : " + e.Message + ", url : " + threadParam.mFullURL; logInfo(info); if (mFrameLogSystem != null && threadParam.mLogError) { mFrameLogSystem.logHttpOverTime(info); } } finally { ThreadListLock.waitForUnlock(); mHttpThreadList.Remove(threadParam.mThread); ThreadListLock.unlock(); } }
//-------------------------------------------------------------------------------------------------------------------------------------------------------------- static protected void waitPostHttpWebRequest(object param) { RequestThreadParam threadParam = param as RequestThreadParam; try { //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) Stream newStream = threadParam.mRequest.GetRequestStream(); //创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 newStream.Write(threadParam.mByteArray, 0, threadParam.mByteArray.Length); newStream.Close(); //4. 读取服务器的返回信息 HttpWebResponse response = (HttpWebResponse)threadParam.mRequest.GetResponse(); StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string phpend = php.ReadToEnd(); php.Close(); response.Close(); threadParam.mCallback(JsonMapper.ToObject(phpend), threadParam.mUserData); } catch (Exception e) { threadParam.mCallback(null, threadParam.mUserData); string info = "http post result exception : " + e.Message + ", url : " + threadParam.mFullURL; logInfo(info); if (mFrameLogSystem != null && threadParam.mLogError) { mFrameLogSystem.logHttpOverTime(info); } } finally { ThreadListLock.waitForUnlock(); mHttpThreadList.Remove(threadParam.mThread); ThreadListLock.unlock(); } }
public static JsonData httpWebRequestPost(string url, string param, OnHttpWebRequestCallback callback = null, object callbakcUserData = null, bool logError = true) { // 转换输入参数的编码类型,获取byte[]数组 byte[] byteArray = stringToBytes(param, Encoding.UTF8); // 初始化新的webRequst // 1. 创建httpWebRequest对象 ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); // 2. 初始化HttpWebRequest对象 webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = byteArray.Length; webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.Timeout = 10000; // 异步 if (callback != null) { RequestThreadParam threadParam = new RequestThreadParam(); threadParam.mRequest = webRequest; threadParam.mByteArray = byteArray; threadParam.mCallback = callback; threadParam.mUserData = callbakcUserData; threadParam.mFullURL = url + param; threadParam.mLogError = logError; Thread httpThread = new Thread(waitPostHttpWebRequest); threadParam.mThread = httpThread; httpThread.Start(threadParam); httpThread.IsBackground = true; ThreadListLock.waitForUnlock(); mHttpThreadList.Add(httpThread); ThreadListLock.unlock(); return(null); } // 同步 else { try { // 3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) Stream newStream = webRequest.GetRequestStream(); //创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 newStream.Write(byteArray, 0, byteArray.Length); newStream.Close(); // 4. 读取服务器的返回信息 HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string phpend = php.ReadToEnd(); php.Close(); response.Close(); return(JsonMapper.ToObject(phpend)); } catch (Exception) { return(null); } } }
public static LitJson.JsonData httpWebRequestPost(string url, string param, OnHttpWebRequestCallback callback, object callbakcUserData = null) { // 转换输入参数的编码类型,获取byte[]数组 byte[] byteArray = BinaryUtility.stringToBytes("gamedata=" + param, Encoding.UTF8); // 初始化新的webRequst // 1. 创建httpWebRequest对象 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); // 2. 初始化HttpWebRequest对象 webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = byteArray.Length; webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.Timeout = 5000; // 异步 if (callback != null) { RequestThreadParam threadParam = new RequestThreadParam(); threadParam.mRequest = webRequest; threadParam.mByteArray = byteArray; threadParam.mCallback = callback; threadParam.mUserData = callbakcUserData; Thread httpThread = new Thread(waitPostHttpWebRequest); threadParam.mThread = httpThread; httpThread.Start(threadParam); mHttpThreadList.Add(httpThread); return(null); } // 同步 else { try { //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) Stream newStream = webRequest.GetRequestStream(); //创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 newStream.Write(byteArray, 0, byteArray.Length); newStream.Close(); //4. 读取服务器的返回信息 HttpWebResponse response; response = (HttpWebResponse)webRequest.GetResponse(); StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string phpend = php.ReadToEnd(); php.Close(); response.Close(); return(JsonMapper.ToObject(phpend)); } catch (Exception) { return(null); } } }
static public JsonData httpWebRequestGet(string urlString, OnHttpWebRequestCallback callback = null, bool logError = true) { HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(new Uri(urlString)); //根据url地址创建HTTpWebRequest对象 httprequest.Method = "GET"; httprequest.KeepAlive = false; //持久连接设置为false httprequest.ProtocolVersion = HttpVersion.Version11; // 网络协议的版本 httprequest.ContentType = "application/x-www-form-urlencoded"; //http 头 httprequest.AllowAutoRedirect = true; httprequest.MaximumAutomaticRedirections = 2; httprequest.Timeout = 10000;//设定超时10秒(毫秒) // 异步 if (callback != null) { RequestThreadParam threadParam = new RequestThreadParam(); threadParam.mRequest = httprequest; threadParam.mByteArray = null; threadParam.mCallback = callback; threadParam.mFullURL = urlString; threadParam.mLogError = logError; Thread httpThread = new Thread(waitGetHttpWebRequest); threadParam.mThread = httpThread; httpThread.Start(threadParam); httpThread.IsBackground = true; ThreadListLock.waitForUnlock(); mHttpThreadList.Add(httpThread); ThreadListLock.unlock(); return(null); } // 同步 else { try { HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse(); Stream steam = response.GetResponseStream(); StreamReader reader = new StreamReader(steam, Encoding.UTF8); string pageStr = reader.ReadToEnd(); reader.Close(); response.Close(); httprequest.Abort(); reader = null; response = null; httprequest = null; return(JsonMapper.ToObject(pageStr)); } catch (Exception) { return(null); } } }
public static JsonData httpWebRequestPostFile(string url, List<FormItem> itemList, OnHttpWebRequestCallback callback, object callbakcUserData, bool logError) { // 以模拟表单的形式上传数据 string boundary = "----" + DateTime.Now.Ticks.ToString("x"); string fileFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + "\r\nContent-Type: application/octet-stream" + "\r\n\r\n"; string dataFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"" + "\r\n\r\n{1}"; MemoryStream postStream = new MemoryStream(); foreach (var item in itemList) { string formdata = null; if (item.mFileContent != null) { formdata = string.Format(fileFormdataTemplate, "fileContent", item.mFileName); } else { formdata = string.Format(dataFormdataTemplate, item.mKey, item.mValue); } // 统一处理 byte[] formdataBytes = null; // 第一行不需要换行 if (postStream.Length == 0) { formdataBytes = stringToBytes(formdata.Substring(2, formdata.Length - 2), Encoding.UTF8); } else { formdataBytes = stringToBytes(formdata, Encoding.UTF8); } postStream.Write(formdataBytes, 0, formdataBytes.Length); // 写入文件内容 if (item.mFileContent != null && item.mFileContent.Length > 0) { postStream.Write(item.mFileContent, 0, item.mFileContent.Length); } } // 结尾 byte[] footer = stringToBytes("\r\n--" + boundary + "--\r\n", Encoding.UTF8); postStream.Write(footer, 0, footer.Length); byte[] postBytes = postStream.ToArray(); ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.Method = "POST"; webRequest.ContentType = "multipart/form-data; boundary=" + boundary; webRequest.Timeout = 10000; webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.ContentLength = postBytes.Length; // 异步 if (callback != null) { RequestThreadParam threadParam = new RequestThreadParam(); threadParam.mRequest = webRequest; threadParam.mByteArray = postBytes; threadParam.mCallback = callback; threadParam.mUserData = callbakcUserData; threadParam.mFullURL = url; threadParam.mLogError = logError; Thread httpThread = new Thread(waitPostHttpWebRequest); threadParam.mThread = httpThread; httpThread.Start(threadParam); httpThread.IsBackground = true; ThreadListLock.waitForUnlock(); mHttpThreadList.Add(httpThread); ThreadListLock.unlock(); return null; } // 同步 else { try { // 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) // 创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 Stream newStream = webRequest.GetRequestStream(); newStream.Write(postBytes, 0, postBytes.Length); newStream.Close(); // 读取服务器的返回信息 HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string phpend = php.ReadToEnd(); php.Close(); response.Close(); return JsonMapper.ToObject(phpend); } catch (Exception) { return null; } } }