Example #1
0
    static public LitJson.JsonData httpWebRequestGet(string urlString, OnHttpWebRequestCallback callback)
    {
        System.GC.Collect();
        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;
            Thread httpThread = new Thread(waitGetHttpWebRequest);
            threadParam.mThread = httpThread;
            httpThread.Start(threadParam);
            mHttpThreadList.Add(httpThread);
            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);
            }
        }
    }
Example #2
0
	public static JsonData httpWebRequestPost(string url, string param, string contentType, OnHttpWebRequestCallback callback = null, object callbakcUserData = null, bool logError = true)
	{
		return httpWebRequestPost(url, stringToBytes(param, Encoding.UTF8), contentType, callback, callbakcUserData, logError);
	}
Example #3
0
	public static JsonData httpWebRequestPost(string url, string param, OnHttpWebRequestCallback callback = null, object callbakcUserData = null, bool logError = true)
	{
		return httpWebRequestPost(url, stringToBytes(param, Encoding.UTF8), "application/x-www-form-urlencoded", callback, callbakcUserData, logError);
	}
Example #4
0
	public static JsonData httpWebRequestPost(string url, byte[] data, OnHttpWebRequestCallback callback = null, object callbakcUserData = null, bool logError = true)
	{
		return httpWebRequestPost(url, data, "application/x-www-form-urlencoded", callback, callbakcUserData, logError);
	}
Example #5
0
	public static JsonData httpWebRequestPost(string url, byte[] data, string contentType, OnHttpWebRequestCallback callback, object callbakcUserData, bool logError)
	{
		// 初始化新的webRequst
		// 创建httpWebRequest对象
		ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
		HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
		// 初始化HttpWebRequest对象
		webRequest.Method = "POST";
		webRequest.ContentType = contentType;
		webRequest.ContentLength = data.Length;
		webRequest.Credentials = CredentialCache.DefaultCredentials;
		webRequest.Timeout = 10000;
		// 异步
		if (callback != null)
		{
			RequestThreadParam threadParam = new RequestThreadParam();
			threadParam.mRequest = webRequest;
			threadParam.mByteArray = data;
			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 newStream = webRequest.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面
				newStream.Write(data, 0, data.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;
			}
		}
	}
Example #6
0
	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;
			}
		}
	}