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

  • SEARCH
  • Home
  • 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 7069565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:27:39+00:00 2026-05-28T05:27:39+00:00

I am trying to use java.net to connect to remote URL in Google App

  • 0

I am trying to use java.net to connect to remote URL in Google App Engine for Java.

After connecting I want to fetch the response headers—

I am using the following code (after connecting and retrieving some data)–
Note- ‘client’ represents a HTTPUrlConnection object.

//now get HTTP Response Headers...
for (Entry<String, List<String>> headernew : client.getHeaderFields().entrySet()) {
System.out.println(headernew.getKey() + "=" + headernew.getValue());
String currName = headernew.getKey();
String currValue = headernew.getValue();

Now, since ‘headernew’ is specified with value as List I cant set ‘currValue’ as I have given in above code.

The line of code which first uses ‘headernew’ is taken from an existing SO answer.

Where is the above code wrong? Should I simply set ‘headernew’ as following–

for (Entry<String, String> headernew : client.getHeaderFields().entrySet()) {

Specifically, I want to retrieve the following pieces of data–

Mime type, charset, response header as key value pair, and body of response (declared as ‘byte[]’)

  • 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-05-28T05:27:40+00:00Added an answer on May 28, 2026 at 5:27 am
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.List;
    import java.util.Map.Entry;
    
    public class Test {
    
      public static void run() throws Throwable {
    
        HttpURLConnection client = null;
        OutputStreamWriter wr = null;
        BufferedReader rd = null;
        StringBuilder sb = null;
        String line = null;
    
        try {
    
          URL url = new URL("http://www.google.com");
          client = (HttpURLConnection) url.openConnection();
          client.setRequestMethod("GET");
          client.setDoOutput(true);
          client.setReadTimeout(10000);
    
          client.connect();
          System.out.println(" *** headers ***");
          for (Entry<String, List<String>> headernew : client.getHeaderFields().entrySet()) {
            System.out.println(headernew.getKey() + "=" + headernew.getValue());
          }
    
          System.out.println(" \n\n*** Body ***");
          rd = new BufferedReader(new InputStreamReader(client.getInputStream()));
          sb = new StringBuilder();
    
          while ((line = rd.readLine()) != null) {
            sb.append(line + '\n');
          }
    
          System.out.println("body=" + sb.toString());
    
        } finally {
          client.disconnect();
          rd = null;
          sb = null;
          wr = null;
        }
      }
    
      public static void main(String[] args) throws Throwable {
        run();
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use ServletContext.getResource to retrieve a java.net.url reference to an image file
I am trying use a Java Uploader in a ROR app (for its ease
I'm trying to use the glassfish-maven-plugin ( https://maven-glassfish-plugin.dev.java.net/ ) with GlassFish v3 (I'm on
Details: I'm trying to use Jalapeno framework to connect my RCP app with Cache'
I'm trying to use this tool https://swingexplorer.dev.java.net/ to find some information out about an
I'm trying to use a solution for serializing exceptions using jaxb. ( http://forums.java.net/jive/thread.jspa?messageID=256122 )
I trying to check if an URL which I want to connect to exists
I am trying to use Java's useDelimiter method on it's Scanner class to do
I am a complete JSP beginner. I am trying to use a java.util.List in
I'm trying to find the best way to use Java libraries that were installed

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.