Andrew James

Andrew James

  • NA
  • 4
  • 1.1k

Using HttpClient over an SSH tunnel

Jun 3 2021 5:32 PM
I am struggling with sending a POST to an HTTP server that is internal and can only be accessed via SSH from the outside.
 
What I'm doing is simply entering a number into a textbox then converting it to a byte to be sent with a POST. From the standpoint of the SSH connection it works fine. It's just that it hangs and does nothing after connecting. I'm pretty new to coding something like this.
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     var data = new  
  4.     {  
  5.         Number = Number.Text,  
  6.     };  
  7.     string stringdata = JsonConvert.SerializeObject(data);  
  8.     byte[] bytedata = Encoding.ASCII.GetBytes(stringdata);  
  9.     Button1.Enabled = true;  
  10.     SSHSubmits.SIDSubmitByte(bytedata);  
  11. }  
Then sending it to a function
  1. public static void SIDSubmitByte(byte[] fromSource)  
  2. {  
  3.     using (var sshClient = ClientCreate())  
  4.     {  
  5.         sshClient.Connect();  
  6.         Httpsend(fromSource).Wait();  
  7.         sshClient.Disconnect();  
  8.     }  
  9. }  
The Httpsend looks like this
  1. private static async Task<string> Httpsend(byte[] fromSource)    
  2. {    
  3.     string consortiumPostAddr = "http://127.0.0.1:42069/incoming/1/1/testsid";    
  4.     HttpClient httpclient = new HttpClient();    
  5.     ByteArrayContent byteContent = new ByteArrayContent(fromSource);    
  6.     await Task.Delay(3000);    
  7.     var response = await httpclient.PostAsync(consortiumPostAddr, byteContent);    
  8.     string result = response.Content.ReadAsStringAsync().Result;    
  9.     httpclient.Dispose();    
  10.     return result;    
  11. }