I tried doing a win32 pinvoke version of this as well and I still get 200 OK as status code. But I did find this answer and it should work. http://mail-archives.apache.org/mod_mbox/hc-dev/200501.mbox/%3CA68B76BAB1C27346BE22FED8D74ACE2C0283BB@owm-exc-1.telecomsys.com%3E
Below is the pinvoke code I used for research if you want to see :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace HttpTest
{
class Program
{
const int INTERNET_OPEN_TYPE_DIRECT = 1; // direct to net
const short INTERNET_DEFAULT_HTTP_PORT = 80;
const int INTERNET_SERVICE_HTTP = 3;
const uint INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP = 0x00008000;
const uint INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS = 0x00004000;
const uint INTERNET_FLAG_KEEP_CONNECTION = 0x00400000;
const uint INTERNET_FLAG_NO_AUTH = 0x00040000;
const uint INTERNET_FLAG_NO_AUTO_REDIRECT = 0x00200000;
const uint INTERNET_FLAG_NO_COOKIES = 0x00080000;
const uint INTERNET_FLAG_NO_UI = 0x00000200;
const uint INTERNET_FLAG_RELOAD = 0x80000000;
const int HTTP_QUERY_STATUS_CODE = 19;
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr InternetOpen(string lpszAgent, int dwAccessType, string lpszProxyName, string lpszProxyBypass, int dwFlags);
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr InternetConnect(IntPtr hInternet, string lpszServerName, short nServerPort, string lpszUsername, string lpszPassword, int dwService, int dwFlags, IntPtr dwContext);
[DllImport("wininet.dll", SetLastError = true)]
static extern IntPtr HttpOpenRequest(
IntPtr hConnect,
string lpszVerb,
string lpszObjectName,
string lpszVersion,
string lpszReferer,
string[] lplpszAcceptTypes,
uint dwFlags,
IntPtr dwContext);
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool HttpSendRequest(IntPtr hRequest, IntPtr lpszHeaders, uint dwHeadersLength, IntPtr lpOptional, uint dwOptionalLength);
[DllImport("wininet.dll", SetLastError = true)]
static extern bool HttpQueryInfo(IntPtr hInternet, int dwInfoLevel, IntPtr lpBuffer, ref long lpdwBufferLength, ref long lpdwIndex);
static void Main(string[] args)
{
IntPtr openPtr = InternetOpen("Kyle", INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
IntPtr context = IntPtr.Zero;
IntPtr connectPtr = InternetConnect(openPtr, "www.youtube.com", INTERNET_DEFAULT_HTTP_PORT, null, null, INTERNET_SERVICE_HTTP, 0, context);
IntPtr openRequestPtr = HttpOpenRequest(connectPtr, "GET", "/", null, null, null,
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_AUTO_REDIRECT |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI |
INTERNET_FLAG_RELOAD, IntPtr.Zero);
if (HttpSendRequest(openRequestPtr, IntPtr.Zero, 0, IntPtr.Zero, 0))
{
IntPtr buffer = Marshal.AllocHGlobal(4); long lBufferLen = 4; long lHeaderIndex = 0;
HttpQueryInfo(openRequestPtr, HTTP_QUERY_STATUS_CODE, buffer, ref lBufferLen, ref lHeaderIndex);
string str2 = Marshal.PtrToStringAnsi(buffer);
}
}
}
}