0

我在执行以下代码时遇到问题。我也有执行休息方法 DELETE 和 PUT 的相同功能,它们在相同的配置下工作得很好,但事实并非如此。

public void Rest_POST(String cookie)
{
String urlString = "https://localhost:8443/rest/api/2/version";
    HttpsURLConnection urlConnection;
    String cookie = getCookie();
     String JSONString = "{" +
             "\"description\":\"An excellent version\"," +
             "\"name\":\"New Version 54164987\"," +
             "\"project\":\"zhelp\"" +
             "}";
URL url;
    OutputStream os;
    HttpsURLConnection connection = null;
    try {
        //this skips the certification check
        certificateManager.skipCertificateCheck();
        url = new URL(urlString);
        connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setRequestProperty("Content-Language", "en-US");
        if(cookie != null) {
            System.out.println("Cookie : " + cookie.split(";", 2)[0]);
            connection.setRequestProperty("Cookie", cookie.split(";", 2)[0]);
        }

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Accept", "application/json");
        connection.setAllowUserInteraction(true);

        os = connection.getOutputStream();
        System.out.println("JSONString : " + JSONString);
        os.write(JSONString.getBytes());
        os.flush();

        System.out.println("Connection : " + connection.getURL());
  }

这会生成错误 FileNotfound 异常。

Error Message: https://localhost:8443/rest/api/2/version
Cause: java.io.FileNotFoundException: https://localhost:8443/rest/api/2/version
4

1 回答 1

1

我是java的新手可能这个解决方案对你有用

先给

connection.setDoOutput(true); connection.setDoInput(true);

如果未解决,则表示任何其他服务使用您的端口 8443,此时资源可通过浏览器获得,但不能通过 Java 代码获得。停止冲突的服务或更改服务器的端口号一切正常

于 2013-08-22T13:02:33.293 回答