开发者

How To Set useUnsafeHeaderParsing For .NET Compact Framework

开发者 https://www.devze.com 2023-02-07 03:25 出处:网络
In my Windows CE 6.0 app, I am communicating with a proprietary web server device that is returning bad header information (more specifically, it\'s returning NO header information).

In my Windows CE 6.0 app, I am communicating with a proprietary web server device that is returning bad header information (more specifically, it's returning NO header information).

I believe this lack of header information is the reason why my HttpWebRequest methods are not working properly.

开发者_JS百科

I recall that the .NET "regular" Framework allows for us to programmatically configure the System.Net.Configuration assembly to allow for invalid headers (useUnsafeHeaderParsing).

Unfortunately, for me, the System.Net.Configuration assembly is not included in the Compact Framework.

Is there a similar configuration in CF that is exposed that allows us to programmatically allow for invalid headers?


I was unable to find a work-around for setting the UseUnsafeHeaderParsing. I decided to remove the implementation of the HttpWebRequest class and use the TcpClient instead. Using the TcpClient class will ignore any problems that may exist with the HTTP Headers - the TcpClient doesn't even think in those terms.

Anyway, using the TcpClient I am able to get the data (including the HTTP Headers) from the proprietary web server that I mentioned in my original post .

For the record, here is a sample of how to retrieve data from a web server via the TcpClient:

The code below is essentially sending a client side HTTP Header packet to a web server.

static string GetUrl(string hostAddress, int hostPort, string pathAndQueryString)
{
string response = string.Empty;

//Get the stream that will be used to send/receive data
TcpClient socket = new TcpClient();
socket.Connect(hostAddress, hostPort);
NetworkStream ns = socket.GetStream();    

//Write the HTTP Header info to the stream
StreamWriter sw = new StreamWriter(ns);
sw.WriteLine(string.Format("GET /{0} HTTP/1.1", pathAndQueryString));
sw.Flush();

//Save the data that lives in the stream (Ha! sounds like an activist!)
string packet = string.Empty;
StreamReader sr = new StreamReader(ns);
do
{
packet = sr.ReadLine();
response += packet;
}
while (packet != null);

socket.Close();

return (response);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号