.NET Compact Framework 中带有 POST 参数的 WebRequest

发布于 2024-10-09 02:32:52 字数 1872 浏览 4 评论 0原文

我正在尝试在 .NET Compact Framework 上执行 HTTP POST REQUEST,但无法使其正常工作。

这就是我得到的:

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

...但由于某些原因它总是发送 0 长度的二进制数据。该代码似乎在 WinForms 和网页上运行良好,但在 CF 上则不然。

知道我的代码出了什么问题或者我忘记了什么吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-10-16 02:32:52

我没有看到您设置 request.ContentLength。这是我使用的、我知道有效的代码:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文