使用HttpsURLConnection&发布JSON OutputStreamWriter失败

时间:2013-08-17 17:28:46

标签: json post processing httpsurlconnection

我正在使用Processing 2.0.2并导入java的java.netjavax.netjava.io类。

我正在尝试将JSONObject发送到“https://www.bitstamp.net/api/balance/”。

JSONObject包含登录信息。我用这个函数创建它:

JSONObject BitstampLoginJSON() {
JSONObject loginJSON = new JSONObject();
loginJSON.setString("password", "mypassword");
loginJSON.setString("user", "username");  
return loginJSON;
}

我成功设置了连接,然后将连接传递给此函数以发送请求。

void SendBitstampBalanceRequestFromConnection(HttpsURLConnection givenCon) {
try {
givenCon.setDoOutput(true);
givenCon.setDoInput(true);    
givenCon.setRequestProperty("Content-Type", "application/json");  
givenCon.setRequestProperty("Acccept", "application/json");
givenCon.setRequestMethod("POST");   
givenCon.connect();
OutputStreamWriter requestSender = new OutputStreamWriter(givenCon.getOutputStream());       
String JSONString = BitstampLoginJSON().toString();        
requestSender.write(JSONString);    
requestSender.flush(); 
requestSender.close();
} 
catch(IOException e) {
e.printStackTrace();
}
}

我收到响应代码200并返回“OK”消息。 Bitstamp返回一个JSON对象,上面写着:

  

“错误”:“缺少用户和/或密码POST参数”}

我很困惑为什么Bitstamp没有接收或接受我的JSONObject。我已经阅读了大约有关于JSON和POSTing的六个帖子,但我似乎无法得到它。

0 个答案:

没有答案