-1

Can I send a packet of data (not a file) to a remote server using HTTP Post in C#? If so, how would the command look? If not, what would be the best way to send this packet? This packet consists of random character data, along with a 2-byte header the lenght of the data and the operation to be performed and is built inside the solution that is sending it to the server. I assume that I would use a GET command to get the server response.

  • Possible duplicate of [Send Raw IP packet in C#, everything above the ethernet layer](https://stackoverflow.com/questions/5652226/send-raw-ip-packet-in-c-everything-above-the-ethernet-layer) – ztadic91 Nov 26 '17 at 15:01
  • I'm not sure question is about raw packets. Few standardised http libraries exist, for example https://msdn.microsoft.com/pl-pl/library/system.net.http.httpclient(v=vs.110).aspx – Jacek Cz Nov 26 '17 at 15:06
  • Your question is a bit vague. You should describe better what the server is expecting and the general context. You could edit your question and add more details (ie. if your data need encoding and/or if you have to authenticate. – Jimi Nov 26 '17 at 19:52

2 Answers2

0

You want to use UploadData on a new System.Net.WebClient which allows you to post arbitrary data (not necessarily a file)

MSDN - WebClient.UploadData Method

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
0

This is a generic method.
Suppose you have a [Data] packet to send:

HttpWebRequest httpRequest  = WebRequest.CreateHttp("Login Url");
httpRequest.Method = WebRequestMethods.Http.Post;
httpRequest.ContentType = "*The content type, if necessary*";
httpRequest.ContentLength = [Data].Length;

// Other Headers configuration if needed

using (Stream _stream = httpRequest.GetRequestStream())
{
   _stream.Write([Data], 0, [Data].Length);
}

using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
{
      // Get the response from the server
}
Jimi
  • 29,621
  • 8
  • 43
  • 61