简体   繁体   English

Rackspace电子邮件API示例C#-无法编译

[英]Rackspace Email API Example C# - Won't Compile

The Rackspace Email API example for C# doesn't work, it is based on OLD code, C#的Rackspace Email API示例不起作用,它基于OLD代码,

http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page

I answer this item below, posted this question for others having the same issue I did. 我在下面回答此问题,并向与我有相同问题的其他人发布了此问题。

UPDATE - Per Basti M comment below, here is the original code. 更新-下面的Per Basti M评论,这是原始代码。

This code does not compile: 此代码无法编译:

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace RestApiClient
{
    public class RestApiClient
    {
        private HttpWebRequest request;
        private HttpWebResponse response;
        private string baseUrl;
        private string apiKey;
        private string secretKey;

        public RestApiClient(string baseUrl, string apiKey, string secretKey)
        {
            this.baseUrl = baseUrl;
            this.apiKey = apiKey;
            this.secretKey = secretKey;
        }

        public HttpWebResponse Get(string url, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "GET";
            SignMessage();
            AssignFormat(format);
            return GetResponseContent();
        }

        public HttpWebResponse Post(string url, string data, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "POST";
            SignMessage();
            AssignFormat(format);
            SendFormData(data);
            return GetResponseContent();
        }

        public HttpWebResponse Put(string url, string data, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "PUT";
            SignMessage();
            AssignFormat(format);
            SendFormData(data);
            return GetResponseContent();
        }

        public HttpWebResponse Delete(string url, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "DELETE";
            SignMessage();
            AssignFormat(format);
            return GetResponseContent();
        }

        private void SendFormData(string data)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteData = encoding.GetBytes(data);
            this.request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteData.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteData, 0, byteData.Length);
            requestStream.Close();
        }

        private HttpWebResponse GetResponseContent()
        {
            try
            {
                return (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                return (HttpWebResponse)e.Response;
            }

        }

        private void SignMessage()
        {
            var userAgent = "C# Client Library";
            this.request.UserAgent = userAgent;
            var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
            var dataToSign = apiKey + userAgent + dateTime + secretKey;
            var hash = SHA1.Create();
            var signedBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
            var signature = Convert.ToBase64String(signedBytes);

            request.Headers["X-Api-User-Signature"] = apiKey + ":" + dateTime + ":" + signature;
        }

        private void AssignFormat(string format)
        {
            this.request.Accept = format;
        }
    }
}

I just wanted to post this working example for everyone: http://www.solutionevangelist.com/post/8 我只想向所有人发布此工作示例: http : //www.solutionevangelist.com/post/8

Just a few slight adjustments, copied here in case link ever dies: 只需稍作调整,如果链接消失了,请复制到此处:

UPDATE : I added additional PUT & DELETE methods. 更新 :我添加了其他的PUT&DELETE方法。

using System;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Text;
using System.Net;
static void Main()
{
    // see: http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page
    var apiKey = "";
    var secretKey = "";
    var wm = new WebMethods(new System.Net.WebClient(), "https://api.emailsrvr.com/v0/", apiKey, secretKey);
    Console.Write(wm.Get("domains")); // Get list of Domains
    Console.Write(wm.Get("domains/example.com")); // Get summary of services in Domain
    Console.Write(wm.Get("domains/example.com/ex/mailboxes")); // Get Rackpsace Email Mailboxes for Domain
    Console.Write(wm.Get("domains/example.com/rs/mailboxes")); // Get Microsoft Exchange Mailboxes for Domain
}
public class WebMethods
{
    private WebClient client;
    private string baseUrl;
    private string apiKey;
    private string secretKey;
    private string format;

    public WebMethods(WebClient client, string baseUrl, string apiKey, string secretKey, string format = "text/xml")
    {
        this.client = client;
        this.baseUrl = baseUrl;
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.format = format;
    }

    public virtual string Get(string url)
    {
        return MakeRemoteCall((client) =>
        {
            return client.DownloadString(baseUrl + url);
        },
        format);
    }

    public virtual string Post(string url, System.Collections.Specialized.NameValueCollection data)
    {
        return MakeRemoteCall((client) =>
        {
            var bytes = client.UploadValues(baseUrl + url, data);
            return Encoding.UTF8.GetString(bytes);
        },
        format);
    }

    public virtual string Put( string url, NameValueCollection data )
    {
        return MakeRemoteCall( ( client ) =>
        {
            var bytes = client.UploadValues( baseUrl + url, "PUT", data );
            return Encoding.UTF8.GetString( bytes );
        },
          format );
    }
    public virtual string Delete( string url )
    {
        return MakeRemoteCall( ( client ) =>
        {
            return client.UploadString( baseUrl + url, "DELETE", "" );
        },
          format );
    }

    private void SignMessage()
    {
        var userAgent = "C# Client Library";
        client.Headers["User-Agent"] = userAgent;

        var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");

        var dataToSign = apiKey + userAgent + dateTime + secretKey;
        var hash = System.Security.Cryptography.SHA1.Create();
        var signedBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
        var signature = Convert.ToBase64String(signedBytes);

        client.Headers["X-Api-Signature"] = apiKey + ":" + dateTime + ":" + signature;
    }

    private void AssignFormat(string format)
    {
        client.Headers["Accept"] = format;
    }

    private string MakeRemoteCall(Func remoteCall, string format)
    {
        SignMessage();
        AssignFormat(format);
        return remoteCall.Invoke(client);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM