2014-11-04 40 views
0

如何添加參數或額外的行代碼來休息在Java中的調用。Jav休息調用獲取參數

GET /ObjectName HTTP/1.1 
Host: BucketName.s3.amazonaws.com 
Date: date Authorization: authorization string (see Authenticating Requests (AWS Signature Version 
4)) Range:bytes=byte_range 

所設定的方法GET後把/Objectname導致錯誤。

我正在使用的代碼是;

public void getObject() throws Exception 
{ 
String fmt = "EEE, dd MMM yyyy HH:mm:ss "; 

SimpleDateFormat df = new SimpleDateFormat(fmt, Locale.US); 
df.setTimeZone(TimeZone.getTimeZone("GMT")); 
String ob2 = "/bucket/test.txt"; 
String bucket = "/bucket"; 
String method = "GET"; 
String contentMD5 = ""; 
String contentType = ""; 
String date = df.format(new Date()) + "GMT";   

// Generate signature 
StringBuffer buf = new StringBuffer(); 
buf.append(method).append("\n");`enter code here` 
buf.append(contentMD5).append("\n"); 
buf.append(contentType).append("\n"); 
buf.append(date).append("\n"); 
buf.append(ob2); 
String signature = sign(buf.toString()); 

HttpURLConnection httpConn = null; 
URL url = new URL("https」,」s3demo.s3demosite.com",443,bucket); 
httpConn = (HttpURLConnection) url.openConnection(); 
httpConn.setDoInput(true); 
httpConn.setDoOutput(true); 
httpConn.setUseCaches(false); 
httpConn.setDefaultUseCaches(false); 
httpConn.setAllowUserInteraction(true); 
httpConn.setRequestMethod(method); 
httpConn.setRequestProperty("Date", date); 
httpConn.setRequestProperty("Content-Length", "0"); 
String AWSAuth = "AWS " + keyId + ":" + signature; 
httpConn.setRequestProperty("Authorization", AWSAuth); 

// Send the HTTP PUT request. 
int statusCode = httpConn.getResponseCode(); 
InputStream err = httpConn.getErrorStream(); 
InputStream is = null; 
is = httpConn.getInputStream(); 
int ch; 
StringBuffer sb = new StringBuffer(); 
while ((ch = err.read()) != -1) { 
    sb.append((char) ch); 
} 
if ((statusCode/100) != 2) 
{ 
    // Deal with S3 error stream. 
    InputStream in = httpConn.getErrorStream(); 
    System.out.println("Error: "+errorStr); 
} 
else 
{ 
    System.out.println("download worked」); 
} 
} 
+1

您可以顯示目前使用的代碼嗎? – 2014-11-04 07:54:03

+0

你想傳遞一個參數來休息服務嗎? – dnsserver 2014-11-04 08:14:43

回答

0

在REST Services中,您可以通過兩種方式傳遞參數。

  1. 爲路徑變量
  2. 作爲查詢參數。例如:GET /students/tomGET /students?name=tom
+0

我的答案中沒有看到任何Java代碼。 – 2014-11-04 08:47:28

+0

是的。只是我解釋了我們有兩種方法將參數傳遞給REST服務。 – dnsserver 2014-11-04 08:50:57

+0

如果您看到添加的代碼行httpConn.setRequestMethod(method);它只會接受GET POST等。添加路徑變量會導致程序錯誤。 – 2014-11-04 10:40:02