csharp 适用于Windows Phone 8的1-File Robust RESTful API内部层

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 适用于Windows Phone 8的1-File Robust RESTful API内部层相关的知识,希望对你有一定的参考价值。


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace RESTAPI
{
    /// <summary>
    /// Encapsulates functionality to make it possible to make
    /// RESTful API calls on web resources and services
    /// </summary>
    class RESTAPHandler
    {
        public delegate void RESTSuccessCallback(Stream stream);
        public delegate void RESTErrorCallback(String reason);

        public  void get(Uri uri, Dictionary<String,String> extra_headers, RESTSuccessCallback success_callback, RESTErrorCallback error_callback)
        {
            HttpWebRequest request = WebRequest.CreateHttp(uri);
            
            if(extra_headers != null)
            foreach (String header in extra_headers.Keys)
                try
                {
                    request.Headers[header] = extra_headers[header];
                }
                catch (Exception) { }

            request.BeginGetResponse((IAsyncResult result) => {
                HttpWebRequest req = result.AsyncState as HttpWebRequest;
                if (req != null)
                {
                    try
                    {
                        WebResponse response = req.EndGetResponse(result);
                        success_callback(response.GetResponseStream());                        
                    }
                    catch (WebException e)
                    {
                        error_callback(e.Message);
                        return;
                    }
                }
            }, request);
        }

        public void post(Uri uri,  Dictionary<String, String> post_params, Dictionary<String, String> extra_headers, RESTSuccessCallback success_callback, RESTErrorCallback error_callback)
        {
            HttpWebRequest request = WebRequest.CreateHttp(uri);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";

            if (extra_headers != null)
                foreach (String header in extra_headers.Keys)
                    try
                    {
                        request.Headers[header] = extra_headers[header];
                    }
                    catch (Exception) { }


            request.BeginGetRequestStream((IAsyncResult result) =>
            {
                HttpWebRequest preq = result.AsyncState as HttpWebRequest;
                if (preq != null)
                {
                    Stream postStream = preq.EndGetRequestStream(result);

                    StringBuilder postParamBuilder = new StringBuilder();
                    if (post_params != null)
                        foreach (String key in post_params.Keys)
                            postParamBuilder.Append(String.Format("{0}={1}&", key, post_params[key]));

                    Byte[] byteArray = Encoding.UTF8.GetBytes(postParamBuilder.ToString());

                    postStream.Write(byteArray, 0, byteArray.Length);
                    postStream.Close();


                    preq.BeginGetResponse((IAsyncResult final_result) =>
                    {
                        HttpWebRequest req = final_result.AsyncState as HttpWebRequest;
                        if (req != null)
                        {
                            try
                            {
                                WebResponse response = req.EndGetResponse(final_result);
                                success_callback(response.GetResponseStream());
                            }
                            catch (WebException e)
                            {
                                error_callback(e.Message);
                                return;
                            }
                        }
                    }, preq);
                }
            }, request);            
        }
    }
}

以上是关于csharp 适用于Windows Phone 8的1-File Robust RESTful API内部层的主要内容,如果未能解决你的问题,请参考以下文章

xml 适用于Windows 8.1和Windows Phone 8.1应用程序的扩展器控件的模板。

适用于 Windows Phone 8 的 MediaElement 速度比

适用于 Windows Phone 8 的 Visual Studio Express 2012 - 它在哪里?

适用于 Windows Phone 8.1 的 XMPP 库

适用于 Windows Phone 8.1 的后台媒体播放器中的无缝循环

Windows Phone 8 保存数据