如何为.NET Compact Framework设置useUnsafeHeaderParsing

3
在我的Windows CE 6.0 应用程序中,我正在与一个专有的Web服务器设备进行通信,该设备返回错误的头信息(更具体地说,它返回了没有头信息)。
我认为这种缺乏头信息是导致我的HttpWebRequest方法无法正常工作的原因。
我记得.NET“常规”框架允许我们以编程方式配置System.Net.Configuration程序集,以允许使用无效标头(useUnsafeHeaderParsing)。
不幸的是,对于我来说,Compact Framework中没有包含System.Net.Configuration程序集。
在CF中是否有类似的配置暴露出来,允许我们以编程方式允许无效标头?
1个回答

7

我无法找到设置UseUnsafeHeaderParsing的解决方法。我决定删除HttpWebRequest类的实现,改用TcpClient。使用TcpClient类将忽略可能存在的HTTP标头问题 - TcpClient甚至不会考虑这些问题。

无论如何,使用TcpClient,我可以从我在原始帖子中提到的专有Web服务器获取数据(包括HTTP标头)。

以下是通过TcpClient从Web服务器检索数据的示例:

以下代码基本上是向Web服务器发送客户端HTTP标头数据包。

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);
}

谢谢你为我节省了很多时间! - Alex S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接