محاولة إرسال البريد والحصول دائمًا على طلب سيئ

0

أحاول إرسال المشاركة على النحو التالي:

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
  • التفويض: Basic 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

يجب عدم استخدام 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"));
:مؤلف

أسئلة ذات صلة

فوق
قائمة طعام