【问题标题】:How To Set useUnsafeHeaderParsing For .NET Compact Framework如何为 .NET Compact Framework 设置 useUnsafeHeaderParsing
【发布时间】:2011-02-02 05:38:37
【问题描述】:

在我的 Windows CE 6.0 应用程序中,我正在与返回错误标头信息(更具体地说,它返回 NO 标头信息)的专有 Web 服务器设备通信。

我认为缺少标头信息是我的 HttpWebRequest 方法无法正常工作的原因。

我记得 .NET“常规”框架允许我们以编程方式配置 System.Net.Configuration 程序集以允许无效标头 (useUnsafeHeaderParsing)。

不幸的是,对我来说,System.Net.Configuration 程序集不包含在 Compact Framework 中。

CF 中是否有类似的公开配置允许我们以编程方式允许无效标头?

【问题讨论】:

    标签: compact-framework httpwebrequest windows-ce


    【解决方案1】:

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

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

    为了记录,这里是一个如何通过 TcpClient 从 Web 服务器检索数据的示例:

    下面的代码本质上是将客户端的 HTTP Header 数据包发送到 Web 服务器。

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

    【讨论】:

    • 感谢您为我节省了大量时间!
    猜你喜欢
    • 2015-11-29
    • 1970-01-01
    • 2020-12-09
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    相关资源
    最近更新 更多