0

所以我正在制作一个程序,您可以在其中输入一些信息。一部分信息需要大量文本,我们说的是 100 多个字符。我发现当数据很大时,它根本不会发送数据。这是我正在使用的代码:

    public void HttpPost(string URI, string Parameters)
    {
        // this is what we are sending
        string post_data = Parameters;

        // this is where we will send it
        string uri = URI;

        // create a request
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri); 
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.Method = "POST";

        // turn our request string into a byte stream
        byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

        // this is important - make sure you specify type this way
        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();
    }

然后我像这样调用该方法:

 HttpPost(url, "data=" + accum + "&pass=HRS");

'accum' 是我发送的大量数据。如果我发送少量数据,此方法有效。但是当它很大时,它不会发送。有没有办法向我网站上的 .php 页面发送超过 100 多个字符的发布请求?

谢谢。

4

2 回答 2

4

只是在打电话GetRequestStream。这不会发出请求 - 默认情况下它将缓冲在内存中,IIRC。

您需要调用WebRequest.GetResponse()以实际向 Web 服务器发出请求。

因此,将代码的结尾更改为:

 // Using statement to auto-close, even if there's an exception
 using (Stream requestStream = request.GetRequestStream())
 {
     requestStream.Write(postBytes, 0, postBytes.Length);
 }

 // Now we're ready to send the data, and ask for a response
 using (WebResponse response = request.GetResponse())
 {
     // Do you really not want to do anything with the response?
 }
于 2012-05-13T07:13:06.823 回答
0

我正在使用这种方式在请求中发布 JSON 数据,我想这有点不同,但它可能会起作用,

httpWebRequest = (HttpWebRequest)WebRequest.Create(RequestURL);
httpWebRequest.ContentType = "application/json";
                httpWebRequest.Accept = "application/json";
                httpWebRequest.Method = "POST";
String username = "UserName";
String password = "passw0rd";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
 using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{" +
                                    "\"user\":[ \"" + user + "\"] " +
                                    "}";
                    sw.Write(json);
                    sw.Flush();
                    sw.Close();
                }
using (HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                {


                    //var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var responseText = streamReader.ReadToEnd();
                        //Now you have your response.
                        //or false depending on information in the response
                    }

                    httpResponse.Close();
                }
于 2016-09-09T18:01:08.643 回答