Question

My app targets Windows CE; it uses the Compact Framework

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?)"

I do have a "using System.Net;"

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

The code is:

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

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

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?

UPDATE

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"

UPDATE 2

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

request.Method = "POST";

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;
        }
    }
}
Was it helpful?

Solution

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.

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;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top