试图发送帖子并总是得到错误的请求

时间:2018-06-19 09:37:40

标签: java api post request

我试图发送如下帖子:

curl -X POST https://app.referralsaasquatch.com/api/v1/{tenant_alias}/rewards/account/{accountId}/user/{userId} \
       -u :API_KEY \
       -H "Content-Type: application/json" \
       -d '{
   "type": "CREDIT",
   "cancellable": true,
   "unit": 'credit-in-cents',
   "assignedCredit": 150*100,
   "currency": EUR
}'
  • API_KEY:API_KEY_VALUE
  • TENANT_ALIAS:test_ai38kl5mysg8n
  • 授权:基本Base64.encode(API_KEY)

    public static void executePost() {
    
    // TODO code application logic here
    String STRURL = "https://app.referralsaasquatch.com/api/v1/" + "test_ai38kl5mysg8n" + "/rewards/account/"
            + ActionGetRsqInfo.IDINTIFIRE + "/user/" + ActionGetRsqInfo.IDINTIFIRE;
    
    System.out.println(STRURL);
    
    URL url;
    HttpURLConnection urlConn = null;
    StringBuilder sb = new StringBuilder();
    
    try {
    
        DataOutputStream printout;
        url = new URL(STRURL);
        urlConn = (HttpURLConnection) url.openConnection();
    
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestMethod("POST");
        urlConn.setRequestProperty("Content-Type", "application/json");
        urlConn.setRequestProperty("API_KEY", "API_KEY_VALUE");
        urlConn.connect();
    
    
    
        JSONObject json = new JSONObject();
    
         json.put("assignedCredit","1500");
         json.put("cancellable", "true");
         json.put("name", "Dollar Credit");
         json.put("type", "CREDIT");
         json.put("unit", "CENTS");
    
          // Send POST output.
            printout = new DataOutputStream(urlConn.getOutputStream());
            printout.writeBytes(URLEncoder.encode(json.toString(), "UTF-8"));
            printout.flush();
            printout.close();
            int HttpResult = ((HttpURLConnection) urlConn).getResponseCode();
    
            if (HttpResult == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
    
                System.out.println( sb.toString());
    
              }
    
            else {
                System.out.println("bad URL    " + ((HttpURLConnection) urlConn).getResponseMessage());
    
            }
    
        }
    
        catch (MalformedURLException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        finally {
            if (urlConn != null)
                urlConn.disconnect();
        }
    
      }
    

1 个答案:

答案 0 :(得分:0)

您不应该使用HttpURLConnection连接到https,尝试使用HttpsURLConnection或其他支持https的客户端

URL urlConnection = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection)urlConnection.openConnection();

也可以尝试使用

  OutputStreamWriter printout= new OutputStreamWriter(connection.getOutputStream());
  printout.write(requestObject.toString());

而不是

 printout = new DataOutputStream(urlConn.getOutputStream());
 printout.writeBytes(URLEncoder.encode(json.toString(), "UTF-8"));