2011-01-24 30 views
4

我['M目前使用這樣的事情httpURLConnection:post參數可以多久?

HttpURLConnection con = (HttpURLConnection) u.openConnection(); 
    con.setDoInput(true); 
    con.setRequestMethod("POST"); 

    con.setDoInput (true); 
    con.setDoOutput (true); 
    con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); 

     out = new DataOutputStream(con.getOutputStream()); 
    String content = "username=" + URLEncoder.encode ("bob") 
     + "&password=" + URLEncoder.encode ("smith"); 
    System.out.println("\n" + "sending form to HTTP server ..."); 
    out.writeBytes (content); 
    out.flush(); 
    out.close(); 

    con.connect(); 

有了這個,我管理的一些數據傳遞到我的服務器。我現在徘徊的是多少可以這樣發送?

我想能夠發送一些xml文件(100〜200行長),並想知道我是否可以做到這一點?

Jason

+0

你實際上應該使用`multipart/form-data`編碼。另請參閱http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – BalusC 2011-01-24 12:15:36

回答

5

帖子正文(通常不稱爲參數,因爲這通常意味着它通過URL傳遞)可以是任何長度,僅受配置限制。

由於POST用於實現文件上傳,大多數系統都允許相當大的機構。 100-200行應該不是問題,除了那裏最偏執的配置。

2

任何長度,只要記住您的請求可以超時。 GET數據被限制爲4096字節。

+0

GET限制主要取決於所使用的webbrowser,它並不明確4096更重要的是,HTTP規範聲明瞭255的安全限制。另請參閱http://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request – BalusC 2011-01-24 12:16:44

2

帖子的最大長度通常在服務器配置中配置,而不是在客戶端配置。