[英]Sending large amounts of POST data
So i'm making a program where you input some information. 所以我正在编写一个程序,您可以在其中输入一些信息。 One part of the information requires alot of text, we are talking 100+ characters. 信息的一部分需要大量文本,我们正在说100多个字符。 What I found is when the data is to large it won't send the data at all. 我发现,当数据过大时,它根本不会发送数据。 Here is the code I am using: 这是我正在使用的代码:
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();
}
I am then calling that method like so: 然后,我像这样调用该方法:
HttpPost(url, "data=" + accum + "&pass=HRS");
'accum' is the large amount of data that I am sending. “累计”是我正在发送的大量数据。 This method works if I send a small amount of data. 如果我发送少量数据,则此方法有效。 But then when it's large it won't send. 但是,当它很大时,它不会发送。 Is there any way to send a post request to a .php page on my website that can exceed 100+ characters? 有什么方法可以将发帖请求发送到我网站上的.php页面,并且超过100个字符?
Thanks. 谢谢。
You're only calling GetRequestStream
. 您只在调用GetRequestStream
。 That won't make the request - by default it will be buffered in memory, IIRC. 那不会发出请求-默认情况下,它将被缓冲在内存中,即IIRC。
You need to call WebRequest.GetResponse()
to actually make the request to the web server. 您需要调用WebRequest.GetResponse()
才能真正向Web服务器发出请求。
So change the end of your code to: 因此,将代码结尾更改为:
// 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?
}
I'm using this way to post JSON data inside the request , I guess it's a little bit different but it may work , 我正在使用这种方式在请求内发布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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.