RESTFUL jetty service returning 403 error java










1















I am facing this HTTP 403 Forbidden response from a https REST service when I am trying to call it from my java code. Can you kindly let me know if I am missing something here?



Please note that the server returns the expected data when I trigger the request from any browser / SOAPUI/ Chrome Postman clients.
2 peer certificates are used - as shown in the ssl info from soapui after the request is sent.



The code snippet is attached. [The headers I set in the code are taken from the request header I found from the successful requests]



HttpsURLConnection connection = (HttpsURLConnection)new URL("https://server address").openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding","gzip, deflate, br");
connection.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1");
connection.addRequestProperty("Connection", "Keep-Alive");
connection.addRequestProperty("Cache-Control","no-cache");
connection.setRequestMethod("GET");
connection.connect();

System.out.println("Response Code : " + connection.getResponseCode()+" "+connection.getResponseMessage());


Response Code : 403 Forbidden










share|improve this question
























  • Since you get a 403 Forbidden it may be that the URL you are requesting is not correct. May be the log files of the rest service show some more information. Could you post them ?

    – tom1299
    Nov 15 '18 at 13:06















1















I am facing this HTTP 403 Forbidden response from a https REST service when I am trying to call it from my java code. Can you kindly let me know if I am missing something here?



Please note that the server returns the expected data when I trigger the request from any browser / SOAPUI/ Chrome Postman clients.
2 peer certificates are used - as shown in the ssl info from soapui after the request is sent.



The code snippet is attached. [The headers I set in the code are taken from the request header I found from the successful requests]



HttpsURLConnection connection = (HttpsURLConnection)new URL("https://server address").openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding","gzip, deflate, br");
connection.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1");
connection.addRequestProperty("Connection", "Keep-Alive");
connection.addRequestProperty("Cache-Control","no-cache");
connection.setRequestMethod("GET");
connection.connect();

System.out.println("Response Code : " + connection.getResponseCode()+" "+connection.getResponseMessage());


Response Code : 403 Forbidden










share|improve this question
























  • Since you get a 403 Forbidden it may be that the URL you are requesting is not correct. May be the log files of the rest service show some more information. Could you post them ?

    – tom1299
    Nov 15 '18 at 13:06













1












1








1


1






I am facing this HTTP 403 Forbidden response from a https REST service when I am trying to call it from my java code. Can you kindly let me know if I am missing something here?



Please note that the server returns the expected data when I trigger the request from any browser / SOAPUI/ Chrome Postman clients.
2 peer certificates are used - as shown in the ssl info from soapui after the request is sent.



The code snippet is attached. [The headers I set in the code are taken from the request header I found from the successful requests]



HttpsURLConnection connection = (HttpsURLConnection)new URL("https://server address").openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding","gzip, deflate, br");
connection.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1");
connection.addRequestProperty("Connection", "Keep-Alive");
connection.addRequestProperty("Cache-Control","no-cache");
connection.setRequestMethod("GET");
connection.connect();

System.out.println("Response Code : " + connection.getResponseCode()+" "+connection.getResponseMessage());


Response Code : 403 Forbidden










share|improve this question
















I am facing this HTTP 403 Forbidden response from a https REST service when I am trying to call it from my java code. Can you kindly let me know if I am missing something here?



Please note that the server returns the expected data when I trigger the request from any browser / SOAPUI/ Chrome Postman clients.
2 peer certificates are used - as shown in the ssl info from soapui after the request is sent.



The code snippet is attached. [The headers I set in the code are taken from the request header I found from the successful requests]



HttpsURLConnection connection = (HttpsURLConnection)new URL("https://server address").openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding","gzip, deflate, br");
connection.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1");
connection.addRequestProperty("Connection", "Keep-Alive");
connection.addRequestProperty("Cache-Control","no-cache");
connection.setRequestMethod("GET");
connection.connect();

System.out.println("Response Code : " + connection.getResponseCode()+" "+connection.getResponseMessage());


Response Code : 403 Forbidden







java rest https






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:59









Rakesh

1,0201618




1,0201618










asked Nov 15 '18 at 12:39









RXDRXD

61




61












  • Since you get a 403 Forbidden it may be that the URL you are requesting is not correct. May be the log files of the rest service show some more information. Could you post them ?

    – tom1299
    Nov 15 '18 at 13:06

















  • Since you get a 403 Forbidden it may be that the URL you are requesting is not correct. May be the log files of the rest service show some more information. Could you post them ?

    – tom1299
    Nov 15 '18 at 13:06
















Since you get a 403 Forbidden it may be that the URL you are requesting is not correct. May be the log files of the rest service show some more information. Could you post them ?

– tom1299
Nov 15 '18 at 13:06





Since you get a 403 Forbidden it may be that the URL you are requesting is not correct. May be the log files of the rest service show some more information. Could you post them ?

– tom1299
Nov 15 '18 at 13:06












2 Answers
2






active

oldest

votes


















0














 Can you please check the Server URL if it is in the Java Acceptable format?

 Sometimes java need escape characters to recognize strings correctly.
This question: What are all the escape characters? , can help you to check if you are using any of those characters. Also check if the conversion in the function is done properly.

  Also, if you have more complex URL, consider to use java.net.URL .

  Finally, check the user agent parameter Setting user agent of a java URLConnection .






share|improve this answer
































    0














    Thanks for your response. The issue is with session cookie to be used for the connection. We are able to connect and get the response back with response code HTTP 200 once the cookie with JSESSIONID is passed as a header. Thanks again.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319714%2frestful-jetty-service-returning-403-error-java%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














       Can you please check the Server URL if it is in the Java Acceptable format?

       Sometimes java need escape characters to recognize strings correctly.
      This question: What are all the escape characters? , can help you to check if you are using any of those characters. Also check if the conversion in the function is done properly.

        Also, if you have more complex URL, consider to use java.net.URL .

        Finally, check the user agent parameter Setting user agent of a java URLConnection .






      share|improve this answer





























        0














         Can you please check the Server URL if it is in the Java Acceptable format?

         Sometimes java need escape characters to recognize strings correctly.
        This question: What are all the escape characters? , can help you to check if you are using any of those characters. Also check if the conversion in the function is done properly.

          Also, if you have more complex URL, consider to use java.net.URL .

          Finally, check the user agent parameter Setting user agent of a java URLConnection .






        share|improve this answer



























          0












          0








          0







           Can you please check the Server URL if it is in the Java Acceptable format?

           Sometimes java need escape characters to recognize strings correctly.
          This question: What are all the escape characters? , can help you to check if you are using any of those characters. Also check if the conversion in the function is done properly.

            Also, if you have more complex URL, consider to use java.net.URL .

            Finally, check the user agent parameter Setting user agent of a java URLConnection .






          share|improve this answer















           Can you please check the Server URL if it is in the Java Acceptable format?

           Sometimes java need escape characters to recognize strings correctly.
          This question: What are all the escape characters? , can help you to check if you are using any of those characters. Also check if the conversion in the function is done properly.

            Also, if you have more complex URL, consider to use java.net.URL .

            Finally, check the user agent parameter Setting user agent of a java URLConnection .







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 19:59

























          answered Nov 15 '18 at 19:43









          ssavva05ssavva05

          2519




          2519























              0














              Thanks for your response. The issue is with session cookie to be used for the connection. We are able to connect and get the response back with response code HTTP 200 once the cookie with JSESSIONID is passed as a header. Thanks again.






              share|improve this answer



























                0














                Thanks for your response. The issue is with session cookie to be used for the connection. We are able to connect and get the response back with response code HTTP 200 once the cookie with JSESSIONID is passed as a header. Thanks again.






                share|improve this answer

























                  0












                  0








                  0







                  Thanks for your response. The issue is with session cookie to be used for the connection. We are able to connect and get the response back with response code HTTP 200 once the cookie with JSESSIONID is passed as a header. Thanks again.






                  share|improve this answer













                  Thanks for your response. The issue is with session cookie to be used for the connection. We are able to connect and get the response back with response code HTTP 200 once the cookie with JSESSIONID is passed as a header. Thanks again.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 9:35









                  RXDRXD

                  61




                  61



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319714%2frestful-jetty-service-returning-403-error-java%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto