WebRequest with POST-parameters in .NET Compact Framework

风格不统一 提交于 2019-12-09 23:55:44

问题


I'm trying to do an HTTP POST REQUEST on .NET Compact Framework and I can't get it working.

This is what I got:

public static string DoPost(string url)
    {
        // initialize from variables
        string responseString = string.Empty;
        ASCIIEncoding encoding = new ASCIIEncoding();
       //// UTF8Encoding encoding = new UTF8Encoding();
        HttpWebResponse response;
        byte[] data = encoding.GetBytes("dummy");
        StreamReader reader;
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

        //do the processing
        SetRequestProperties(request, "POST"); // SETTING METHOD TO POST HERE
        request.GetRequestStream().Write(data, 0, data.Length);
        request.GetRequestStream().Close();
        response = (HttpWebResponse)request.GetResponse();
        reader = new StreamReader(response.GetResponseStream());
        responseString = reader.ReadToEnd();


        //clean up
        response.Close();
        response.GetResponseStream().Close();
        reader.Close();
        reader = null;
        response = null;
        request = null;
        encoding = null;

        //return
        MessageBox.Show("POST SUCCESS");
        return responseString;

    }   


private static void SetRequestProperties(HttpWebRequest request, string s)
    {
        request.Method = s;
        request.AllowWriteStreamBuffering = true;
        request.KeepAlive = false;
        request.ContentType = "application/x-www-form-urlencoded";
        request.SendChunked = false;
        request.Credentials = CredentialCache.DefaultCredentials;
        request.UserAgent = "my mobile user agent";
        request.Timeout = 60000;
        request.ProtocolVersion = new System.Version("1.1");
    }

...but for some reasons it always send 0 length of binary data. The code seems to be working fine with WinForms and Webpages, but not on CF.

Any Idea what´s wrong or what I forget in my code?

Thanks


回答1:


I don't see you setting the request.ContentLength. Here's code that I use that I know works:

private string SendData(string method, string directory, string data)
{
    string page = string.Format("http://{0}/{1}", DeviceAddress, directory);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page);
    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;
    request.Method = method;

    CredentialCache creds = GenerateCredentials();
    if (creds != null)
    {
        request.Credentials = creds;
    }

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

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

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

    Stream requestStream = request.GetRequestStream();

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

    HttpWebResponse response;

    response = (HttpWebResponse)request.GetResponse();

    return GetResponseData(response);
}

public string Post(string directory, string data)
{
    return SendData("POST", directory, data);
}


来源:https://stackoverflow.com/questions/4529547/webrequest-with-post-parameters-in-net-compact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!