2017-03-02 90 views
1

我試圖在以下代碼中使用HttpClient將數據發送到我的服務器。帶有長URI的Android HttpPost

new Thread(new Runnable() { 
    @Override 
    public void run() { 
     try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(URL_PREFIX + "?" + URLEncodedUtils.format(nameValuePairs, "utf-8")); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     } 
     catch (Exception ex) { 
     ex.printStackTrace(); 
     } 
    } 
    }).start(); 

但是當我嘗試發佈這個數據到服務器,我得到一個404錯誤代碼,請求失敗,當我嘗試使用瀏覽器我得到以下檢查鏈接。

Request-URI Too Long 
The requested URL's length exceeds the capacity limit for this server. 
Additionally, a 414 Request-URI Too Long error was encountered while trying to use an ErrorDocument to handle the request. 
when check my httppost uri link length was 3497 char. 

在我的代碼我嘗試發送base64編碼的圖像和其他一些參數。任何人都可以幫我解決這個問題嗎?或以任何其他方式來實現它?

回答

1

你不應該把params放到POST請求的url中,把它放在body裏。

HttpPost httpPost = new HttpPost(URL_PREFIX); 
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 

GET請求參數轉到url。
POST請求參數通常會發送到主體。