我正在尝试执行PATCH请求。 c#中的一切都运行良好,但它在Java中不起作用。
源代码:
String url = postURL;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("X-HTTP-Method-Override", "PATCH");
con.setRequestProperty("Authorization", authInfo);
con.setRequestProperty("PublicKey", publicKey);
con.setRequestProperty("Content-Type", contenttype);
String toPost = "{\"StockQuantity\":"+newVal+"}";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(toPost);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + toPost);
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();
System.out.println(response.toString());
我收到错误:
线程中的异常" main" java.io.FileNotFoundException:https://www.pyewxo-onlineshop.de/odata/ProductVariantAttributeCombination(1757)
完整堆栈跟踪:
线程中的异常" main" java.io.FileNotFoundException:https://www.xsadsaaf-onlineshop.de/odata/ProductVariantAttributeCombination(1757) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 方法)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在 sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1926) 在 sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1921) 在java.security.AccessController.doPrivileged(Native Method)at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 在 communication.ServerCommunication.reqPost(ServerCommunication.java:150) 在xsadsaaf.xsadsaaf.main(xsadsaaf.java:48)引起: java.io.FileNotFoundException: https://www.xsadsaaf-onlineshop.de/odata/ProductVariantAttributeCombination(1757) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1872) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) 在 java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) 在 communication.ServerCommunication.reqPost(ServerCommunication.java:144) ......还有1个
回复代码是:404
使用相同的url,我可以用C#做一个请求,为什么不用Java?