Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:01:47+00:00 2026-06-09T21:01:47+00:00

The scenario is as below. I get a request on my server, do some

  • 0

The scenario is as below.
I get a request on my server, do some processing on it and then I need to place request on another server based on my processing, then build response on bases of what I get from the remote server.
Its to be done in JAVA Playframework 2.0 and I’m missing on part of sending request and getting response from remote server.
Any help would be appreciated.
Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:01:51+00:00Added an answer on June 9, 2026 at 9:01 pm

    Preparing

    We first need to know at least the URL and the charset. The parameters are optional and depends on the functional requirements.

    String url = "http://example.com";
    String charset = "UTF-8";
    String param1 = "value1";
     String param2 = "value2";
    // ...
     String query = String.format("param1=%s&param2=%s", 
     URLEncoder.encode(param1, charset), 
     URLEncoder.encode(param2, charset));
    

    The query parameters must be in name=value format and be concatenated by &. You would normally also URL-encode the query parameters with the specified charset using URLEncoder#encode().

    The String#format() is just for convenience. I prefer it when I would need the String concatenation operator + more than twice.

    Firing an HTTP GET request with (optionally) query parameters:

    It’s a trivial task. It’s the default request method.

     URLConnection connection = new URL(url + "?" + query).openConnection();
     connection.setRequestProperty("Accept-Charset", charset);
     InputStream response = connection.getInputStream();
     // ...
    

    Any query string should be concatenated to the URL using ?. The Accept-Charset header may hint the server what encoding the parameters are in. If you don’t send any query string, then you can leave the Accept-Charset header away. If you don’t need to set any headers, then you can even use the URL#openStream() shortcut method.

      InputStream response = new URL(url).openStream();
      // ...
    

    Either way, if the other side is a HttpServlet, then its doGet() method will be called and the parameters will be available by HttpServletRequest#getParameter().
    Firing a HTTP POST request with query parameters:

    Firing an HTTP POST request with query parameters:

    Setting the URLConnection#setDoOutput() to true implicitly sets the request method to POST. The standard HTTP POST as web froms do is of type application/x-www-form-urlencoded wherein the query string is written to the request body.

     URLConnection connection = new URL(url).openConnection();
     connection.setDoOutput(true); // Triggers POST.
     connection.setRequestProperty("Accept-Charset", charset);
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" +    charset);
     OutputStream output = null;
     try {
        output = connection.getOutputStream();
        output.write(query.getBytes(charset));
      } finally {
       if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
      }
      InputStream response = connection.getInputStream();
      // ...
    

    Note: whenever you’d like to submit a HTML form programmatically, don’t forget to take the name=value pairs of any elements into the query string and of course also the name=value pair of the element which you’d like to “press” programmatically (because that’s usually been used in the server side to distinguish if a button was pressed and if so, which one).

    You can also cast the obtained URLConnection to HttpURLConnection and use its HttpURLConnection#setRequestMethod() instead. But if you’re trying to use the connection for output you still need to set URLConnection #setDoOutput() to true.

      HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
      httpConnection.setRequestMethod("POST");
      // ...
    

    Either way, if the other side is a HttpServlet, then its doPost() method will be called and the parameters will be available by HttpServletRequest#getParameter().

    By the way Its almost a copy paste from following question

    Using java.net.URLConnection to fire and handle HTTP requests

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the scenario below, how can I get references to the variables declared during
I have some scenario like below : /** * @ORM\Entity * @ORM\Table(name=role) */ class
I am trying get the Regex right for the following scenario but have some
In the scenario below, I have a boolean value. Depending on the result, I
I'm looking for example of how I would solve the scenario below: Imagine my
I was expecting the below scenario common, but couldn't find much help online. I
I was wondering if the below scenario will work? I am having trouble with
Please suggest me the best authentication way to implement in the scenario mentioned below:
I am new to Regexp. I am struck in writing regexp for below scenario.
How can I check the duplicate signatures before adding. In the below scenario, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.