StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
soooulp  
#1 Posted : Thursday, November 3, 2022 3:26:10 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

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

thanks 1 user thanked soooulp for this useful post.
Charles on 11/3/2022(UTC)
Rob  
#2 Posted : Thursday, November 3, 2022 12:47:01 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Do you get any kind of script errors?
soooulp  
#3 Posted : Thursday, November 3, 2022 1:00:50 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
UserPostedImage

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

Rob  
#4 Posted : Thursday, November 3, 2022 1:13:21 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
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

Rob  
#5 Posted : Thursday, November 3, 2022 1:21:21 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
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

soooulp  
#6 Posted : Thursday, November 3, 2022 1:25:50 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

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

UserPostedImage

Edited by user Thursday, November 3, 2022 1:50:47 PM(UTC)  | Reason: Not specified

Rob  
#7 Posted : Thursday, November 3, 2022 1:35:41 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Hmm, that doesn't seem to make sense Laugh

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.

thanks 1 user thanked Rob for this useful post.
soooulp on 11/3/2022(UTC)
Users browsing this topic
Guest
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.