public static async Task <SocketPipe> ConnectAsync(string host, int port, bool tls = false) { var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); // TODO (pri 3): all this TLS code is not tested // TODO (pri 3): would be great to get flat APIs for TLS SslStream tlsStream = null; if (tls) { var networkStream = new NetworkStream(socket, FileAccess.Read, false); tlsStream = new SslStream(networkStream); await tlsStream.AuthenticateAsClientAsync(host).ConfigureAwait(false); } else { await socket.ConnectAsync(host, port).ConfigureAwait(false); } var client = new SocketPipe(socket, tlsStream); client._responseReader = client.ReceiveAsync(); client._requestWriter = client.SendAsync(); return(client); }
static async Task Main(string[] args) { var enrollmentNumber = ConfigurationManager.AppSettings["enrollment"]; var accessToken = ConfigurationManager.AppSettings["token"]; _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/csv")); _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); // var responseStream = await _httpClient.GetStreamAsync($"enrollments/{enrollmentNumber}/usagedetails/download?billingPeriod=201902"); // var responseMessage = await _httpClient.GetAsync($"enrollments/{enrollmentNumber}/usagedetails/download?billingPeriod=201902"); // var pipeHttpClient = new PipeHttpClient( ); IPHostEntry ipHostInfo = Dns.GetHostEntry("consumption.azure.com"); using (var clientSocketPipe = await SocketPipe.ConnectAsync(ipHostInfo.HostName, 443, false)) { var pipeHttpClient = new PipeHttpClient(clientSocketPipe); // var request = new GetFileRequest($"v3/enrollments/{enrollmentNumber}/usagedetails/download?billingPeriod=201902", accessToken); var request = new GetFileRequest($"v3/enrollments/{enrollmentNumber}/billingPeriods", accessToken); try { StorageResponse response = await pipeHttpClient.SendRequest <GetFileRequest, StorageResponse>(request); if (response.StatusCode != 200) { } } catch (Exception ex) { var msg = ex.Message; } //ulong bytesToRead = response.ContentLength; //using (var file = new FileStream(localFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous)) //{ // await file.WriteAsync(response.Body, bytesToRead); //} } }
//internal Sha256 Hash => _hash; public async ValueTask <StorageResponse> SendRequest <TRequest>(TRequest request) where TRequest : IStorageRequest { if (!_client.IsConnected) { _pipe = await SocketPipe.ConnectAsync(_host, _port).ConfigureAwait(false); _pipe.Log = Log; _client = new PipeHttpClient(_pipe); } request.Client = this; StorageResponse response = await _client.SendRequest <TRequest, StorageResponse>(request).ConfigureAwait(false); if (request.ConsumeBody) { await ConsumeResponseBody(response.Body); } return(response); }