2012-05-13 56 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(); 
       }