Skip to content

Commit 6c90a57

Browse files
committed
Update for SSL
1 parent 93a581a commit 6c90a57

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

TranslateXML/Translate.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public string Translate(string sourceText, string langFrom, string langTo)
6666
GetToken();
6767
string txtToTranslate = sourceText;
6868
string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(txtToTranslate) + "&from=" + langFrom + "&to=" + langTo;
69+
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
70+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
6971
WebRequest translationWebRequest = WebRequest.Create(uri);
7072
translationWebRequest.Headers.Add("Authorization", token);
7173
WebResponse response = translationWebRequest.GetResponse();
@@ -88,6 +90,8 @@ public List<string> GetLanguagesForTranslate()
8890
{
8991
GetToken();
9092
string uri = "http://api.microsofttranslator.com/v2/Http.svc/GetLanguagesForTranslate";
93+
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
94+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
9195
WebRequest languagesWebRequest = WebRequest.Create(uri);
9296
languagesWebRequest.Headers.Add("Authorization", token);
9397
WebResponse response = languagesWebRequest.GetResponse();
@@ -107,6 +111,8 @@ public List<string> GetLanguageNames(List<string> languageList)
107111
{
108112
GetToken();
109113
string uri = "http://api.microsofttranslator.com/v2/Http.svc/GetLanguageNames?locale=" + CultureInfo.CurrentCulture.Name;
114+
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
115+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
110116
WebRequest languagesWebRequest = WebRequest.Create(uri);
111117
languagesWebRequest.Headers.Add("Authorization", token);
112118
languagesWebRequest.ContentType = "text/xml";
@@ -251,6 +257,8 @@ public string Translate(string sourceText, string langFrom, string langTo)
251257
////string url = String.Format("https://translate.google.co.uk/#{0}/{1}/{2}", langFrom, langTo, HttpUtility.UrlEncode(sourceText));
252258
//string url = String.Format("https://translate.google.com/?hl=en&eotf=1&sl={0}&tl={1}&q={2}", langFrom, langTo, HttpUtility.UrlEncode(sourceText));
253259

260+
//WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
261+
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
254262
//WebRequest translationWebRequest = WebRequest.Create(url);
255263
//WebResponse response = translationWebRequest.GetResponse();
256264
//Stream stream = response.GetResponseStream();
@@ -350,6 +358,8 @@ public JsonWeb SearchRequest(string search)
350358
queryString["safesearch"] = "Moderate";
351359
string uri = "https://api.cognitive.microsoft.com/bing/v7.0/search?" + queryString;
352360

361+
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
362+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
353363
HttpResponseMessage response = clientSearch.GetAsync(uri).Result;
354364
Stream stream = response.Content.ReadAsStreamAsync().Result;
355365
return (JsonWeb)jsonSerializer.ReadObject(stream);
@@ -385,6 +395,8 @@ public JsonWeb SpellRequest(string checkText, eSpellMode spellMode)
385395
//content.Headers.ContentLength = byteData.Length;
386396
//clientSpell.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
387397

398+
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
399+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
388400
HttpResponseMessage response = clientSpell.PostAsync(uri, content).Result;
389401
Stream stream = response.Content.ReadAsStreamAsync().Result;
390402
return (JsonWeb)jsonSerializer.ReadObject(stream);
@@ -404,6 +416,8 @@ public string TranslateRequestAsync(string from, string to, string text)
404416

405417
try
406418
{
419+
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
420+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
407421
HttpResponseMessage response = clientTranslate.PostAsync(uri, new StringContent(requestBody, Encoding.UTF8, "application/json")).Result;
408422
string result = response.Content.ReadAsStringAsync().Result;
409423
TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject<TranslationResult[]>(result);
@@ -427,6 +441,8 @@ public Dictionary<string, string> AvailableLanguagesRequestAsync()
427441

428442
try
429443
{
444+
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
445+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
430446
HttpResponseMessage response = clientTranslate.GetAsync(uri).Result;
431447
string result = response.Content.ReadAsStringAsync().Result;
432448
AvailableLanguagesResult deserializedOutput = JsonConvert.DeserializeObject<AvailableLanguagesResult>(result);

0 commit comments

Comments
 (0)