ApiCore.ApiRequest.Send C# (CSharp) Method

Send() public static method

Send request to API
public static Send ( string url ) : string
url string Url to request
return string
        public static string Send(string url)
        {
            try
            {
                ApiRequest.request = (HttpWebRequest)HttpWebRequest.Create(url);
                ApiRequest.request.UserAgent = "xternalx vkontakte api wrapper/0.1a";
                ApiRequest.request.Timeout = ApiRequest.Timeout;
                ApiRequest.request.Method = "GET";

                ApiRequest.response = (HttpWebResponse)ApiRequest.request.GetResponse();

                Stream responseStream = ApiRequest.response.GetResponseStream();

                if (responseStream == null)
                {
                    throw new ApiRequestNullResult("No content was been downloaded");
                }
                else
                {
                    StreamReader sr = new StreamReader(responseStream);

                    return sr.ReadToEnd();
                }
            }
            catch(Exception e)
            {
                throw new ApiRequestNullResult("Unknown result: "+e.Message);
            }
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Execute method
        /// </summary>
        /// <returns>himself</returns>
        public ApiManager Execute()
        {
            //try
            //{
            //BackgroundWorker bw = new BackgroundWorker();
            //bw.DoWork += (o, e) =>
            //    {
            this.apiResponseXml = null;
            string req = this.builder.BuildQuery();

            this.OnLog("Request string: " + req);
            ApiRequest.Timeout     = this.Timeout;
            this.apiResponseString = ApiRequest.Send(req);
            this.debugMsg(this.apiResponseString);
            if (!this.apiResponseString.Equals("") || this.apiResponseString.Length > 0)
            {
                this.apiResponseXml = new XmlDocument();
                this.apiResponseXml.LoadXml(this.apiResponseString);
                XmlNode isError = this.apiResponseXml.SelectSingleNode("/error");
                if (isError == null)
                {
                    this.MethodSuccessed = true;
                }
                else
                {
                    int         code    = Convert.ToInt32(isError.SelectSingleNode("error_code").InnerText);
                    string      msg     = isError.SelectSingleNode("error_msg").InnerText;
                    Hashtable   ht      = new Hashtable();
                    XmlNodeList pparams = isError.SelectNodes("request_params/param");
                    foreach (XmlNode n in pparams)
                    {
                        ht[n.SelectSingleNode("key").InnerText.ToString()] = n.SelectSingleNode("value").InnerText.ToString();
                    }

                    throw new ApiRequestErrorException("Server error occurred", code, msg, ht);
                }
                //return this;
            }
            else
            {
                throw new ApiRequestEmptyAnswerException("API Server returns an empty answer or request timeout");
            }

            //};
            //bw.RunWorkerCompleted += (o, e) =>
            //    {

            //    }
            //bw.WorkerSupportsCancellation = true;
            //bw.RunWorkerAsync();
            return(this);
            //}
            //catch(Exception e)
            //{
            //    throw new ApiRequestNullResult("source message: "+e.Message);
            //}
        }
All Usage Examples Of ApiCore.ApiRequest::Send
ApiRequest