【发布时间】:2020-03-05 23:07:04
【问题描述】:
我正在尝试使用 Java 通过 API 创建一篇 Wordpress 帖子。 到目前为止,我有以下代码:
public class ConnessioneHttp {
public void connect() throws Exception{
URL url = new URL(Constants.API_POST);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer " + Constants.BEARER_TOKEN);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
String params = "?title=test from Java&content=lorem ipsum&status=publish";
try(DataOutputStream wr = new DataOutputStream(conn.getOutputStream())){
wr.writeBytes(parametri);
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + params);
System.out.println("Response Code : " + responseCode);
try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line);
}
System.out.println(response.toString());
}
}
}
我调用它的方法是一个包含以下代码的操作按钮:
@Override
public void actionPerformed(ActionEvent e) {
ConnessioneHttp conn = new ConnessioneHttp();
try{
conn.connect();
} catch (Exception err){
err.printStackTrace();
}
}
我已经尝试发出 GET 请求并且它工作正常,所以我认为错误不在我用来调用 HttpRequest 方法 (ConnessioneHttp) 的方法中。
我尝试过使用 Postman 和 post 请求
"http://localhost/project/wp-json/wp/v2/posts?title=test from Java&content=lorem ipsum&status=publish"
工作正常,所以我认为错误也不在参数中。
这是我第一次使用 HTTP 请求和 Wordpress,所以我确定有错误,我只是不知道我错过了什么
这是我得到的输出:
Sending 'POST' request to URL : http://localhost/tesi/wp-json/wp/v2/posts
Post parameters : ?title=test da JAVA&content=lorem ipsum&status=publish
Response Code : 400
java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost/tesi/wp-json/wp/v2/posts
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 it.unibas.progettotesi.modello.ConnessioneHttp.connect(ConnessioneHttp.java:55)
at it.unibas.progettotesi.controllo.ControlloPanelPrincipale$AzioneHttp.actionPerformed(ControlloPanelPrincipale.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost/tesi/wp-json/wp/v2/posts
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 it.unibas.progettotesi.modello.ConnessioneHttp.connect(ConnessioneHttp.java:49)
... 38 more
【问题讨论】:
-
首先您可以通过传递适当的参数在 PostMan REST 客户端中尝试,如果可以,您可以编写代码。