2014-11-14 45 views
1

我使用下面的java代碼創建了Web服務調用。現在我需要進行刪除和放置操作。在HttpURLConnection中發送PUT,刪除HTTP請求

URL url = new URL("http://example.com/questions"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setDoOutput(true); 
conn.setRequestMethod("POST"); 
conn.setRequestProperty("Content-Type", "application/json"); 

OutputStream os = conn.getOutputStream(); 
os.write(jsonBody.getBytes()); 
os.flush(); 

當我添加以下代碼來執行DELETE操作它給錯誤說:

java.net.ProtocolException:HTTP方法刪除不支持輸出。

conn.setRequestMethod("DELETE"); 

那麼如何進行刪除,並把請求?

+0

請原諒喊,但**沒有給出什麼**錯誤?另外,我會建議['HttpClient'](http://hc.apache.org/httpcomponents-client-ga/index.html)來自apache。 –

+0

編輯了這個問題。任何Web服務請求的示例代碼?任何幫助? – manitaz

+0

是否http://stackoverflow.com/questions/1051004/how-to-send-put-delete-http-request-in-httpurlconnection幫助? – hgoebl

回答

1

我建議你使用的Restlet客戶端的Web服務請求。請參閱波紋管的示例代碼,它可以使用HttpURLConnection幫助你

Client client = new Client(new Context(), Protocol.HTTP); 
    clientResource = new ClientResource(url); 
     ResponseRepresentation responseRep = null; 
     try { 
      clientResource.setNext(client);   
      clientResource.delete(); 

     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
+0

如何設置內容類型和內容?什麼是不同的HttpUrlConnection和這個? – manitaz

+0

http://stackoverflow.com/questions/23630094/how-does-restlet-client-handle-media-type-of-request請參閱此鏈接的內容類型處理 – Jamsheer

+0

讓我檢查此鏈接。感謝幫助。 – manitaz

5

PUT例如:

URL url = null; 
try { 
    url = new URL("http://localhost:8080/putservice"); 
} catch (MalformedURLException exception) { 
    exception.printStackTrace(); 
} 
HttpURLConnection httpURLConnection = null; 
DataOutputStream dataOutputStream = null; 
try { 
    httpURLConnection = (HttpURLConnection) url.openConnection(); 
    httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    httpURLConnection.setRequestMethod("PUT"); 
    httpURLConnection.setDoInput(true); 
    httpURLConnection.setDoOutput(true); 
    dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream()); 
    dataOutputStream.write("hello"); 
} catch (IOException exception) { 
    exception.printStackTrace(); 
} finally { 
    if (dataOutputStream != null) { 
     try { 
      dataOutputStream.flush(); 
      dataOutputStream.close(); 
     } catch (IOException exception) { 
      exception.printStackTrace(); 
     } 
    } 
    if (httpsURLConnection != null) { 
     httpsURLConnection.disconnect(); 
    } 
} 

DELETE例如使用HttpURLConnection

URL url = null; 
try { 
    url = new URL("http://localhost:8080/deleteservice"); 
} catch (MalformedURLException exception) { 
    exception.printStackTrace(); 
} 
HttpURLConnection httpURLConnection = null; 
try { 
    httpURLConnection = (HttpURLConnection) url.openConnection(); 
    httpURLConnection.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
    httpURLConnection.setRequestMethod("DELETE"); 
    System.out.println(httpURLConnection.getResponseCode()); 
} catch (IOException exception) { 
    exception.printStackTrace(); 
} finally {   
    if (httpURLConnection != null) { 
     httpURLConnection.disconnect(); 
    } 
} 
+0

在刪除我需要使用httpURLConnection.setRequestProperty(「Content-Type」,「application/json」);這可能嗎? – manitaz

+0

是的 - 有可能 – Jamsheer

+0

java.net.ProtocolException:HTTP方法DELETE不支持輸出 – manitaz

1

PUT

URL url = new URL("http://www.example.com/resource"); 
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
httpCon.setDoOutput(true); 
httpCon.setRequestMethod("PUT"); 
OutputStreamWriter out = new OutputStreamWriter(
    httpCon.getOutputStream()); 
out.write("Resource content"); 
out.close(); 
httpCon.getInputStream(); 

進行刪除

URL url = new URL("http://www.example.com/resource"); 
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
httpCon.setDoOutput(true); 
httpCon.setRequestProperty(
    "Content-Type", "application/x-www-form-urlencoded"); 
httpCon.setRequestMethod("DELETE"); 
httpCon.connect(); 

其實這樣做是從這裏的鏈接: Send PUT, DELETE HTTP request in HttpURLConnection