c# – 访问Amazon Mechanical Turk最简单的例子是什么?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 访问Amazon Mechanical Turk最简单的例子是什么?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2083字,纯文字阅读大概需要3分钟。
内容图文
![c# – 访问Amazon Mechanical Turk最简单的例子是什么?](/upload/InfoBanner/zyjiaocheng/816/d1f05d593a9c4d6e9edeeba6027682bc.jpg)
我正在寻找一个在C#中访问Amazon Mechanical Turk Web服务的简单示例.我知道有一个用于C#的SDK,但我想在使用之前了解基本的机制.
解决方法:
这是我用来访问Mechanical Turk的最简单的代码示例.它创建签名并调用GetAccountBalance操作.
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace TurkTest {
class Program {
static void Main(string[] args) {
const string SERVICE_NAME = "AWSMechanicalTurkRequester"; // requester service for MTurk
const string TIMESTAMP_FORMAT = "yyyy-MM-ddTHH:mm:ss.fffZ";
// Modify these with your values.
const string operation = "GetAccountBalance";
const string accessKey = "<Your access key>";
const string secretAccessKey = "<Your secret access key>";
// Millisecond values in the timestamp string can result in intermittent BadClaimsSupplied errors.
// Get the current UTC time and use that to create a new time with milliseconds set to zero to avoid this case.
DateTime now = DateTime.UtcNow;
now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, 0, DateTimeKind.Utc);
string timeStamp = now.ToString(TIMESTAMP_FORMAT, CultureInfo.InvariantCulture);
// Create the hash-based messaged authentication algorithm (SHA1) using our secret access key as the key.
var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(secretAccessKey));
// Combine the service name, operation and timestamp and then hash them to produce the signature.
var dataBytes = Encoding.UTF8.GetBytes(SERVICE_NAME + operation + timeStamp);
string signature = Convert.ToBase64String(hmac.ComputeHash(dataBytes));
// Build the URL to send to Amazon
string url =
@"https://mechanicalturk.amazonaws.com/?Service=AWSMechanicalTurkRequester&AWSAccessKeyId={0}&Version=2012-03-25&Operation={1}&Signature={2}&Timestamp={3}";
url = string.Format(url, accessKey, operation, signature, timeStamp);
// Send a request and write the response to the console.
using (WebClient client = new WebClient()) {
using (StreamReader reader = new StreamReader(client.OpenRead(url))) {
Console.WriteLine(reader.ReadToEnd());
}
}
Console.Read();
}
}
}
内容总结
以上是互联网集市为您收集整理的c# – 访问Amazon Mechanical Turk最简单的例子是什么?全部内容,希望文章能够帮你解决c# – 访问Amazon Mechanical Turk最简单的例子是什么?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】