我正在尝试添加网站网址,以便通过Java休息调用在Netsparker中进行扫描。
身份验证已完成,但收到此错误:
Error-
HTTP/1.1 400 Bad Request
X-Frame-Options:
DENY
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Date: Fri, 22 Jan 2016 08:48:51 GMT
Content-Length: 98
X-XSS-Protection: 1; mode=block
Expires: -1
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Server: Apigee Router
X-Content-Type-Options: nosniff
Pragma: no-cache
Cache-Control: no-cache
Authentication request-
PasswordAuthentication (AuthToken, "".toCharArray());
最近他们可能已经更改了他们的API,因此,如果有人曾经使用过这个,并且可以提供示例代码段,那么将不胜感激。 提前谢谢。
答案 0 :(得分:0)
我编写了一个没有任何依赖关系的代码。
用您的api令牌替换YOUR_API_TOKEN。 如果您要创建基于订阅的网站,则应将“Credit”替换为“Subscription”。
package com.netsparkercloud;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
String createWebsiteEndpoint = "https://www.netsparkercloud.com/api/1.0/websites/new";
try {
URL url = new URL(createWebsiteEndpoint);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setRequestProperty("Authorization", "Basic YOUR_API_TOKEN");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.connect();
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write("{\n" +
" \"Name\": \"Java Test\",\n" +
" \"RootUrl\": \"http://test.com\",\n" +
" \"LicenseType\": \"Credit\"\n" +
"}");
wr.flush();
wr.close();
InputStream is;
if (con.getResponseCode() >= 400) {
is = con.getErrorStream();
} else {
is = con.getInputStream();
}
Reader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
for ( int c = in.read(); c != -1; c = in.read() )
System.out.print((char)c);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}