-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathAddTranslationArrayMethod.cs
More file actions
152 lines (135 loc) · 6.31 KB
/
AddTranslationArrayMethod.cs
File metadata and controls
152 lines (135 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Web;
using System.ServiceModel.Channels;
using System.ServiceModel;
namespace MicrosoftTranslatorSdk.HttpSamples
{
class Program
{
static void Main(string[] args)
{
AdmAccessToken admToken;
string headerValue;
//Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/
//Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)
AdmAuthentication admAuth = new AdmAuthentication("clientID", "client secret");
try
{
admToken = admAuth.GetAccessToken();
// Create a header with the access_token property of the returned token
headerValue = "Bearer " + admToken.access_token;
AddTranslationArrayMethod(headerValue);
}
catch (WebException e)
{
ProcessWebException(e);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
private static void AddTranslationArrayMethod(string authToken)
{
string appId = "";
string uri = "http://api.microsofttranslator.com/v2/Http.svc/AddTranslationArray";
string originalText1 = "una importante contribución a la rentabilidad de la empresa";
string translatedText1 = "a significant contribution tothe company profitability";
string originalText2 = "a veces los errores son divertidos";
string translatedText2 = "in some cases errors are fun";
string body = GenerateAddtranslationRequestBody(appId, "es", "en", "general", "text/plain", "", "TestUserId");
string translationsCollection = string.Format("{0}{1}",
GenerateAddtranslationRequestElement(originalText1, 8, 0, translatedText1),
GenerateAddtranslationRequestElement(originalText2, 6, 0, translatedText2));
// update the body
string requestBody = string.Format(body, translationsCollection);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.ContentType = "text/xml";
request.Method = "POST";
request.Headers.Add("Authorization", authToken);
using (System.IO.Stream stream = request.GetRequestStream())
{
byte[] arrBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(requestBody);
stream.Write(arrBytes, 0, arrBytes.Length);
}
// get the response
WebResponse response = null;
try
{
response = request.GetResponse();
using (Stream respStream = response.GetResponseStream())
{
Console.WriteLine(string.Format("Your translations for '{0}' and '{1}' has been added successfully.", originalText1, originalText2));
}
}
catch
{
throw;
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
private static string GenerateAddtranslationRequestBody(string appId, string from, string to, string category, string contentType, string uri, string user)
{
string body = "<AddtranslationsRequest>" +
"<AppId>{0}</AppId>" +
"<From>{1}</From>" +
"<Options>" +
"<Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">{2}</Category>" +
"<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">{3}</ContentType>" +
"<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">{4}</User>" +
"<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">{5}</Uri>" +
"</Options>" +
"<To>{6}</To>" +
"<Translations>{7}</Translations>" +
"</AddtranslationsRequest>";
return string.Format(body, appId, from, category, contentType, user, uri, to, "{0}");
}
private static string GenerateAddtranslationRequestElement(string originalText, int rating, int sequence, string translatedText)
{
string element = "<Translation xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">" +
"<OriginalText>{0}</OriginalText>" +
"<Rating>{1}</Rating>" +
"<TranslatedText>{2}</TranslatedText>" +
"<Sequence>{3}</Sequence>" +
"</Translation>";
return string.Format(element, originalText, rating.ToString(), translatedText, sequence.ToString());
}
private static void ProcessWebException(WebException e)
{
Console.WriteLine("{0}", e.ToString());
// Obtain detailed error information
string strResponse = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)e.Response)
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
{
strResponse = sr.ReadToEnd();
}
}
}
Console.WriteLine("Http status code={0}, error message={1}", e.Status, strResponse);
}
}
}