Java代码:
public class KeyGeneration {
public static void main(String[] args) throws Exception {
System.out.println("Send Http POST request");
sendPost();
}
// HTTP POST request
private static void sendPost() throws Exception {
String url = "http://localhost:4000/channels/mychannel/chaincodes/changedneww741";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("Authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTY3MjE4MDgsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MTY2ODU4MDh9.8iUCGNaLYhC6MFPfnvc9lxk9b9Oi-5OtwLYgF7Pn6a8");
String urlParameters = "peers=[\"localhost:7051\",\"localhost:8051\"]&fcn=move&args=[\"publickey\",\"raj1242\"]";
// Send post request
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 : " + url);
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());
}
}
错误:
java.io.IOException:服务器返回HTTP响应代码:400为URL: http://localhost:4000/channels/mychannel/chaincodes/changedneww741 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 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) 在mailsend.KeyGeneration.sendPost(KeyGeneration.java:131)at mailsend.KeyGeneration.main(KeyGeneration.java:35)引起: java.io.IOException:服务器返回HTTP响应代码:400为URL: http://localhost:4000/channels/mychannel/chaincodes/changedneww741 at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) 在 java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) 在mailsend.KeyGeneration.sendPost(KeyGeneration.java:125)... 1 更