我有一个应该配置的设备。
它有一个WEB Interface
,IP x.x.x.x
,我可以通过IP
中的Browser
连接到设备并执行它们,现在客户想要一个软件来执行此操作。
设备核心是customized Linux
。
我想找到它通过网络界面发送的Services
和Parameters
。
所以我打开web interface
并转到IP
和我想改变的部分。
在web
中更改字段后,我点击了Apply Button
。
我还下载Fiddler
并打开了Developer Tools
FireFox
。
在我点击Developer Tools
后的Apply
中,会出现一个POST method
,其他人都是Get Method
。
此外,在我点击Apply Button
后,实际URL
从x.x.x.x/services
更改为x.x.x.x/apply.cgi
。
所以我似乎可以通过x.x.x.x/apply.cgi
URL来完成它们。
我打开Developer Tools
和POST method
请求标头就像:
Host: x.x.x.x
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://x.x.x.x/Services.asp
Authorization: Basic cm9vdDoxMjM0NQ==
Connection: keep-alive
Upgrade-Insecure-Requests: 1
在Fiddler的TextView
我们有这个:
submit_button=Services&action=ApplyTake&change_action=&submit_type=&commit=1&static_leases=13&openvpn_certtype=&dhcpd_usejffs=&dhcpd_usenvram=0
(It is alot, i just copy part of that )
enter code here
我想我可以写一个简单的调用HttpURLConnection
通过这种方式,这是我的代码:
public class PostHTTPSample {
private String USER_AGENT = "Mozilla/5.0";
String userCredentials = "root:xxxxxx";
public void sendRequestPost() throws IOException {
try {
String encoded = Base64.getEncoder().encodeToString((userCredentials).getBytes(StandardCharsets.UTF_8)); //Java 8
String str = "http://x.x.x.x/apply.cgi";
URL obj = new URL(str);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Accept-Encoding", "gzip, deflate");
con.setRequestProperty("Authorization", "Basic "+"cm9vdDoxMjM0NQ=="); // I even tested encoded String that i has declared and define above
con.setRequestProperty("Host", "x.x.x.x");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Upgrade-Insecure-Requests", "1");
String urlParameters = "The String Copied from Fiddler(Above) For test";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + str);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
} catch (MalformedURLException ex) {
Logger.getLogger(PostHTTPSample.class.getName()).log(Level.SEVERE, null, ex);
}
}
好的,我从这个类中创建一个实例并运行该方法,它给了我Response Code : 400
。
以及Netbeans
的运行控制台:
Feb 01, 2017 3:16:13 PM posthttpsample.PostHTTPSample main
SEVERE: null
java.io.IOException: Server returned HTTP response code: 400 for URL: http://x.x.x.x/apply.cgi
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1921)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at posthttpsample.PostHTTPSample.sendRequestPost(PostHTTPSample.java:58)
at posthttpsample.PostHTTPSample.main(PostHTTPSample.java:82)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://x.x.x.x/apply.cgi
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at posthttpsample.PostHTTPSample.sendRequestPost(PostHTTPSample.java:52)
... 1 more
enter code here
我做错了什么? 我该怎么办? 有什么建议吗?
编辑:我可以使用此网址的GET
方法,它会给我200 OK response
。
但仍然无法使用POST
方法。