【问题标题】:Send PUT, DELETE HTTP request in HttpURLConnection在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求
【发布时间】:2014-11-14 05:41:52
【问题描述】:

我在下面的代码中使用 java 创建了 Web 服务调用。现在我需要执行删除和放置操作。

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();

当我添加下面的代码来执行删除操作时,它会给出错误提示:

java.net.ProtocolException:HTTP 方法 DELETE 不支持输出。

conn.setRequestMethod( "DELETE" );

那么如何执行delete和put请求呢?

【问题讨论】:

  • 请原谅大喊大叫,但它给出了 WHAT 错误?另外,我建议来自 apache 的 HttpClient
  • 编辑了问题。 Web 服务请求的任何示例代码?有什么帮助吗?
  • 我会检查这个,但由于某种原因它对我不起作用。

标签: java web-services http-post httpurlconnection http-delete


【解决方案1】:

PUT 使用HttpURLConnection 的示例:

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();
    }
}

【讨论】:

  • 在删除中我需要使用 httpURLConnection.setRequestProperty("Content-Type", "application/json");有可能吗?
  • java.net.ProtocolException: HTTP 方法 DELETE 不支持输出
  • 这个例子是我发现的第一个有效的删除!
【解决方案2】:

用于放置

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

【讨论】:

    【解决方案3】:

    我建议你使用 restlet 客户端来请求 web 服务。请参考下面的示例代码,它可能会对你有所帮助

     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();
    
           }
    

    【讨论】:

    • 如何设置content-type和内容? HttpUrlConnection 和这个有什么不同?
    • stackoverflow.com/questions/23630094/… 内容类型处理请参考此链接
    • 让我检查一下这个链接。感谢您的帮助。
    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多