StrokesPlus.net Forum
»
General Discussion
»
General Discussion
»
How to send an HTTP request to get the Google translate result through the Proxy?
Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 161 Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
I use a gesture to get and show the selected words translate result ( link), but Google translates service is totally left our Country, I need to modify the Host to access or use the Proxy which I prefer. I find the following which sends a request by the Proxy, and I can't get the result after the gesture. Code:WebProxy proxyObject = new WebProxy("http://proxyserver:80/", true);
// HttpClient lifecycle management best practices:
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
HttpClient client = new HttpClient(new HttpClientHandler
{
Proxy = proxyObject
});
A part of my gesture code Code:
var proxyObject = new System.Net.WebProxy("127.0.0.1:7890", true);
var httpHandler = new HttpClientHandler();
httpHandler.AutomaticDecompression = host.flags(DecompressionMethods.GZip, DecompressionMethods.Deflate);
var client = new HttpClient(httpHandler
{
Proxy = proxyObject
});
var response;
var result;
var json;
var proxy;
client.BaseAddress = new Uri("https://translate.googleapis.com/translate_a/");
response = client.GetAsync("single?client=gtx&sl=zh-CN&tl=en&dt=t&q=" + text).Result;
result = response.Content.ReadAsStringAsync().Result;
json = JSON.parse(result);
Edited by user Thursday, November 3, 2022 1:48:15 PM(UTC)
| Reason: Not specified
|
1 user thanked soooulp for this useful post.
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,359 Location: Tampa, FL Thanks: 28 times Was thanked: 419 time(s) in 356 post(s)
|
Do you get any kind of script errors?
|
|
|
|
Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 161 Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
If it is necessary for the environment to test, my PC can be remoted with some suitable remote program. Edited by user Thursday, November 3, 2022 1:49:47 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,359 Location: Tampa, FL Thanks: 28 times Was thanked: 419 time(s) in 356 post(s)
|
Try this: Code:var text = "test"; //Just so this code is complete by itself
try {
var httpHandler = new HttpClientHandler();
httpHandler.Proxy = new System.Net.WebProxy("127.0.0.1:7890", true);
httpHandler.AutomaticDecompression = host.flags(DecompressionMethods.GZip, DecompressionMethods.Deflate);
var client = new HttpClient(httpHandler);
var response;
var result;
var json;
var proxy;
client.BaseAddress = new Uri("https://translate.googleapis.com/translate_a/");
response = client.GetAsync("single?client=gtx&sl=zh-CN&tl=en&dt=t&q=" + text).Result;
result = response.Content.ReadAsStringAsync().Result;
json = JSON.parse(result);
} finally {
// IMPORTANT! Make sure to dispose these or it can lead to a memory leak
httpHandler.Dispose();
client.Dispose();
}
Edited by user Thursday, November 3, 2022 1:14:15 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,359 Location: Tampa, FL Thanks: 28 times Was thanked: 419 time(s) in 356 post(s)
|
Also, their example has a syntax error: Code:HttpClient client = new HttpClient(new HttpClientHandler
{
Proxy = proxyObject
});
Should be: Code:HttpClient client = new HttpClient(new HttpClientHandler()
{
Proxy = proxyObject
});
I'm not sure if that object initialization syntax would translate into S+ anyway, but it is still funny that they messed up in their documentation. Code://This:
var x = new HttpClientHandler()
{
Proxy = proxyObject
};
//Is the same as:
var x = new HttpClientHandler();
x.Proxy = proxyObject;
Edited by user Thursday, November 3, 2022 1:23:21 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 161 Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
Wow, I just add this line in my original code, and it works well to get the translated result. httpHandler.Proxy = new System.Net.WebProxy("127.0.0.1:7890", true); But when I change to the try…finally…, it will show this ERROR Edited by user Thursday, November 3, 2022 1:50:47 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,359 Location: Tampa, FL Thanks: 28 times Was thanked: 419 time(s) in 356 post(s)
|
Hmm, that doesn't seem to make sense Maybe it has something to do with asynchronous calls? Try putting it after the json line. As long as you're not getting a script error, it would be fine and get disposed.
|
1 user thanked Rob for this useful post.
|
|
|
StrokesPlus.net Forum
»
General Discussion
»
General Discussion
»
How to send an HTTP request to get the Google translate result through the Proxy?
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The StrokesPlus.net Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close