Ask Your Question
1

How to create a tunnel for sending a proxy request in Dart?

asked 2021-11-22 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-10-16 12:00:00 +0000

qstack gravatar image

To create a tunnel for sending a proxy request in Dart, you can use the dart:io library and HttpClient class to create a tunnel through which the proxy request can be sent.

Here is an example code to create a tunnel:

import 'dart:io';

Future<void> main() async {
  final client = HttpClient();
  // create a tunnel through the proxy server
  final conn = await client.open(
    'CONNECT myserver.com:443 HTTP/1.1\r\n'
    'Host: myserver.com:443\r\n'
    'Proxy-Authorization: Basic BASE64_ENCODED_CREDENTIALS\r\n'
    '\r\n',
    'myproxy.com',
    8080
  );  
  // Use the tunnel to send a request to server
  final request = await HttpClient().getUrl(Uri.parse('https://myserver.com/path'));
  final response = await (await conn).send(request);
  final body = await response.stream.bytesToString();
  print(body);
  // close the connection
  await (await conn).close();
}

In this example, we first create an HttpClient instance and then open a connection using the open method. The open method takes three arguments: the first argument is the CONNECT request to create the tunnel, the second argument is the proxy server domain name or IP address, and the third argument is the proxy server port number.

After the tunnel is established, we can use that connection to send a request to our target server using the HttpClient instance. Here we send a GET request to /path on the https://myserver.com. We then wait for the response and read its body.

Finally, we close the connection by calling the close method on the connection object.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-22 11:00:00 +0000

Seen: 10 times

Last updated: Oct 16 '21