How do I add custom content to a HttpWebRequest header in VB.Net

Spike UK
Spike UK used Ask the Experts™
on
I currently have a HttpWebRequest wcustom header written in VB.net defined as:

Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
            request.Method = "POST"
            request.Headers.Add("X-SessionID", sessionID)
            request.ContentType = "application/json; charset=utf-8"

Open in new window


I need to add in the content as below created using PHP/JS.

$options = array(
  'http' =>
  array (
    'method'        => 'POST',
    'header'        => 'X-SessionID: ' . $MY_SESSION_ID . "\r\n" .
                       'Content-Type: application/json',
    'content'       => json_encode($mylist1),
    'ignore_errors'  => true,
  )
);

Open in new window


How do I do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
If you read this MS document carefully, you can find out all the answers: https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest?view=netframework-4.7.2

Your request code is almost complete, missing only the data itself. If you read the example code here:
https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contentlength?view=netframework-4.7.2
You'll see 1 byte being transmitted. Replace it with text instead (so change to Encoding.ASCII.GetString), and the correct content.

Author

Commented:
Very clear- thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial