WebClient真的对.NET 3.5 CF不可用吗? [英] Is WebClient really unavailable to .NET 3.5 CF?

查看:283
本文介绍了WebClient真的对.NET 3.5 CF不可用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序以Windows CE为目标;它使用了Compact Framework

My app targets Windows CE; it uses the Compact Framework

我复制了一些其他使用WebClient的可运行代码,但无法编译,并说:"找不到类型或名称空间名称'WebClient'(您是否缺少using指令或程序集?参考?)"

I copied over some elsewhere-working code that uses WebClient, but it won't compile, saying, "The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)"

我确实有一个正在使用System.Net;"

I do have a "using System.Net;"

由于该错误,我还得到:"名称'HttpRequestHeader'在当前上下文中不存在"

Because of that error, I also get, "The name 'HttpRequestHeader' does not exist in the current context"

代码是:

private static void SendXMLFile(string xmlFilepath, string uri)
{
    using (var client = new WebClient())
    {
        client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        StringBuilder sb = new StringBuilder();
        using (StreamReader sr = new StreamReader(xmlFilepath))
        {
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                sb.AppendLine(line);
            }
        }
        // I don't know why the prepended equals sign is necessary, but it is
        string allLines = "=" + sb.ToString();
        var result = client.UploadString(uri, "POST", allLines);
    }
}

如果WebClient 适用于我的方案,我需要什么参考/使用方式?

If WebClient is available for my scenario, what Reference/using do I need?

如果在我的情况下 无法使用WebClient,那有什么追索权/替代权?

If WebClient is not available for my scenario, what recourse/replacement is there?

如果没有 追索权/替代权,谁能/将给我送来一瓶黑莓白兰地?

If there is no recourse/replacement, who can/will send me a bottle of Blackberry Brandy?

我正在尝试根据情况调整答案,但立即遇到问题:

I'm trying to adapt the answer to my situation, but run into a problem right away:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
request.Method = method;

问题是无法识别方法"(得到红色波浪状的情况,并且"名称方法"在当前上下文中不存在"

The problem is that "method" is not recognized (gets a case of the red squigglys and "The name 'method' does not exist in the current context"

我知道-方法是传入的字符串;我需要的是:

I see - method is a string passed in; what I need is:

request.Method = "POST";

更新3

我不确定这是否会真正起作用,但是它确实可以编译,而且似乎很有意义,所以我将尝试一下:

UPDATE 3

I'm not sure yet if this will actually work, but it does compile, and seems to make sense, so I'll try it out:

private static string SendXMLFile(string xmlFilepath, string uri, int timeout)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;
    request.Method = "POST";

    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
        byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());
        if (timeout < 0)
        {
            request.ReadWriteTimeout = timeout;
            request.Timeout = timeout;
        }

        request.ContentLength = postBytes.Length;
        request.KeepAlive = false;

        request.ContentType = "application/x-www-form-urlencoded";

        try
        {
            Stream requestStream = request.GetRequestStream();

            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                return response.ToString();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            request.Abort();
            return string.Empty;
        }
    }
}

推荐答案

WebClient不存在,尽管实际上并没有那么重要.您可以使用HttpWebRequest来完成大部分工作.

WebClient doesn't exist, though really it's really not all that important to have. You can get by with a HttpWebRequest for most any of what you'll be doing.

遵循这些原则(来自OpenNETCF扩展):

Something along these lines (from the OpenNETCF Extensions):

private string SendData(string method, string directory, string data, string contentType, int timeout)
{
    lock (m_syncRoot)
    {
        directory = directory.Replace('\\', '/');
        if (!directory.StartsWith("/"))
        {
            directory = "/" + directory;
        }

        string page = string.Format("{0}{1}", DeviceAddress, directory);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page);
#if !WINDOWS_PHONE
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
#endif
        request.Method = method;

        // turn our request string into a byte stream
        byte[] postBytes;

        if (data != null)
        {
            postBytes = Encoding.UTF8.GetBytes(data);
        }
        else
        {
            postBytes = new byte[0];
        }

#if !WINDOWS_PHONE
        if (timeout < 0)
        {
            request.ReadWriteTimeout = timeout;
            request.Timeout = timeout;
        }

        request.ContentLength = postBytes.Length;
        request.KeepAlive = false;
#endif
        if (contentType.IsNullOrEmpty())
        {
            request.ContentType = "application/x-www-form-urlencoded";
        }
        else
        {
            request.ContentType = contentType;
        }

        try
        {
            Stream requestStream = request.GetRequestStream();

            // now send it
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();


            using (var response = (HttpWebResponse)request.GetResponse())
            {
                return GetResponseString(response);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            request.Abort();
            return string.Empty;
        }
    }
}

这篇关于WebClient真的对.NET 3.5 CF不可用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆