2012-12-16 18 views
0

我正在嘗試對Fusion Table API進行REST調用。我使用Spring模板爲REST如何在URL中添加授權和內容類型

當我定義我的網址爲:

String URI = "https://www.googleapis.com/upload/fusiontables/v1/tables/tableid/import"; 

例外:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized 

這是很好的,意思是其打擊谷歌服務器。

但是當我使用:

URL :String URI = "https://www.googleapis.com/upload/fusiontables/v1/tables/tableid/import"+"&Authorization:""&Content-Type: application/octet-stream"; 

其投擲execption:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 404 Not Found 

對此我無法理解。

回答

4

您正在將請求標頭附加到您的URL字符串,這是不正確的。這兩個標頭:

  • 授權: 「」
  • 內容類型:應用/八位字節流

應該被添加到請求的URL。相反,如果你使用Apache HttpComponents:

final HttpPost post = new HttpPost(); 
post.addHeader("Authorization", ""); 
post.addHeader("Content-Type", "application/octet-stream"); 

或者,用HttpURLConnection類:

conn.setRequestProperty("Authorization", ""); 
conn.setRequestProperty("Content-Type", "application/octet-stream"); 

或者,同春RestTemplate:

HttpHeaders headers = new HttpHeaders(); 
headers.add("Authorization", ""); 
headers.add("Content-Type", "application/octet-stream"); 
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, headers); 

有一噸的信息,具體的春天他們的文檔頁面上的選項爲RestTemplateHttpEntity

+0

我正在使用spring的休息模板 – bhalkian

+0

當我查看他們的doc(Spring模板)時,我可以找到任何有助於在URL中添加授權的東西。 – bhalkian