发送大量POST数据

时间:2012-05-13 07:04:43

标签: c# http post

所以我正在制作一个你输入一些信息的程序。信息的一部分需要很多文本,我们说的是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'是我发送的大量数据。如果我发送少量数据,此方法有效。但是当它很大时它就不会发送。有没有办法将帖子请求发送到我网站上可能超过100个字符的.php页面?

感谢。

2 个答案:

答案 0 :(得分:4)

呼叫GetRequestStream。这不会发出请求 - 默认情况下它会被缓存在内存中,IIRC。

您需要致电WebRequest.GetResponse()以实际向网络服务器发出请求。

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

 // 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?
 }

答案 1 :(得分: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();
                }