Skip to content

Instantly share code, notes, and snippets.

Created March 18, 2013 18:34
Show Gist options
  • Save anonymous/5189584 to your computer and use it in GitHub Desktop.
Save anonymous/5189584 to your computer and use it in GitHub Desktop.
Flash-Like Unity HTTP Request
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public delegate void SuccessHandler(HTTPRequest request);
public delegate void ErrorHandler(HTTPRequest request);
public class HTTPRequest
{
/**
* Event raised when a request returns successfully
*/
public event SuccessHandler OnSuccess;
/**
* Event raised when a request returns an error
*/
public event ErrorHandler OnError;
/**
* Response body from this request, if any
*/
public string ResponseBody { get; set; }
/**
* The error if any
*/
public string Error {get; set; }
/**
* True iff this is a post request
*/
protected bool m_isPost = false;
/**
* Whether or not this is a post request
*/
public bool IsPost {
get{
return m_isPost;
}set{
m_isPost = value;
}
}
/**
* The params for this request
*/
protected Dictionary<string, string> m_Params;
/**
* The params for this request
*/
public Dictionary<string, string> Parameters {
get {
if (m_Params == null) {
m_Params = new Dictionary<string, string>();
}
return m_Params;
}
set {
m_Params = value;
}
}
/**
* True iff we should attempt to cache bust
*/
protected bool m_CacheBust = true;
/**
* True iff we should attempt to cache bust
*/
public bool CacheBust {
get {
return m_CacheBust;
}
set {
m_CacheBust = value;
}
}
/**
* The url for this request
*/
public string URL { get; set; }
/**
* Underlying WWW request
*/
protected WWW m_WWW;
/**
* Underlying Form, if any
*/
protected WWWForm m_Form;
/**
*
*/
public HTTPRequest(string url){
this.URL = url;
}
/**
* Convenience function for adding params
*/
public void addParam(string param, string val) {
this.Parameters.Add(param, val);
}
public void Perform() {
if (this.m_isPost) {
m_Form = new WWWForm();
foreach (KeyValuePair<string, string> param in this.Parameters) {
m_Form.AddField(param.Key, param.Value);
}
string requestedUrl = URL;
if (this.CacheBust) {
if (!requestedUrl.Contains("?")) {
requestedUrl += "?zzcb=" + new DateTime().Ticks;
} else {
requestedUrl += "&zzcb=" + new DateTime().Ticks;
}
}
m_WWW = new WWW(requestedUrl, m_Form);
} else {
string requestedUrl = URL;
if (!requestedUrl.Contains("?")) {
requestedUrl += "?";
} else {
requestedUrl += "&";
}
foreach (KeyValuePair<string, string> param in this.Parameters) {
requestedUrl += WWW.EscapeURL(param.Key) + "=" + WWW.EscapeURL(param.Value) + "&";
}
if (this.CacheBust) {
requestedUrl += "zzcb=" + new DateTime().Ticks;
} else {
requestedUrl.TrimEnd(new char[] {'&', '?'});
}
m_WWW = new WWW(URL);
}
ServiceLocator.MainGame.StartCoroutine(WaitForRequest(this, m_WWW));
}
IEnumerator<WWW> WaitForRequest(HTTPRequest request, WWW www)
{
yield return www;
if (www.error == null){
request.ResponseBody = www.text;
if (request.OnSuccess != null) {
request.OnSuccess(request);
}
} else {
request.ResponseBody = www.text;
request.Error = www.error;
if (request.OnError != null) {
request.OnError(request);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment