/// <summary> /// Get web response to read from server. /// </summary> /// <param name="result">The async result, internally generated.</param> private static void ResponseOpen(IAsyncResult result) { ApiState state = (ApiState)result.AsyncState; WebResponse response; try { response = state.request.EndGetResponse(result); } catch (WebException e) { state.error_message = e.Message; state.response = (HttpWebResponse)e.Response; state.error(state); return; } state.response = (HttpWebResponse)response; Stream istream = response.GetResponseStream(); int length = (int)istream.Length; byte[] buffer = new byte[length]; istream.Read(buffer, 0, length); state.response_body = Encoding.UTF8.GetString(buffer, 0, length); state.success(state); var hello = EtaSDK.Properties.Resources.Hello; }
/// <summary> /// Request open callback, for post requests to send data. /// </summary> /// <param name="result">The async result, internally generated.</param> private static void RequestOpen(IAsyncResult result) { //System.Resources. ApiState state = (ApiState)result.AsyncState; Stream ostream = null; try { ostream = state.request.EndGetRequestStream(result); } catch (WebException e) { state.error_message = e.Message; state.response = (HttpWebResponse)e.Response; state.error(state); return; } if (state.sdata.Length > 0) { byte[] sendbuffer = System.Text.Encoding.UTF8.GetBytes(state.sdata); ostream.Write(sendbuffer, 0, sendbuffer.GetLength(0)); } ostream.Close(); state.request.BeginGetResponse(new AsyncCallback(Eta.ResponseOpen), state); }
/// <summary> /// The API call /// </summary> /// <param name="url">Url to call, must start with "/api/"</param> /// <param name="options">An EtaApiObjects instance with any data to send</param> /// <param name="success">A method taking a ApiState instance to be called on success.</param> /// <param name="error">A method taking a ApiState instance to be called on success.</param> public void api(string url, EtaApiOptions options, Action <object> success, Action <object> error) { List <KeyValuePair <string, string> > param = this.FormatParams(options.data); string data = this.BuildParams(param); if (!url.StartsWith("/api/")) { throw new EtaException("Illegal URL!"); } options.type = options.type.ToUpper(); ApiState state = new ApiState(); string target = Eta.domain + url; if (options.type == "GET") { target += "?" + data; state.sdata = ""; } else { state.sdata = data; } HttpWebRequest xhr = (HttpWebRequest)System.Net.HttpWebRequest.Create(target); xhr.Accept = this.accepts[options.dataType]; xhr.Method = options.type; state.request = xhr; state.options = options; state.success = success; state.error = error; Debug.WriteLine(target); try { if (options.type == "POST") { xhr.ContentType = options.contentType; xhr.BeginGetRequestStream(new AsyncCallback(Eta.RequestOpen), state); } else { xhr.BeginGetResponse(new AsyncCallback(Eta.ResponseOpen), state); } } catch (Exception e) { throw new EtaException("Could not open connection.\n" + e.Message); } }