2012-05-13 52 views
0

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

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多个字符的发布请求?

谢谢。

+2

你的问题不应该是100个字符的限制,你的accum变量中没有'?'或'&'吗? – Ciro

+0

我不会:\我刚发送了这个http://puu.sh/urKY,它甚至没有发送它。 –

回答

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? 
} 
+0

我不想回应,我只想发送数据。 –

+0

@DuncanPalmer:那么你仍然需要调用'GetResponse'来实际发送数据。因此,使用我发布的代码并忽略响应(除了自动状态代码检查)。请注意,你* do *仍然希望使用'using'语句。 –

+0

好吧,我照你说的做了,它仍然不会发送。 :( –

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