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 9137365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:41+00:00 2026-06-17T09:05:41+00:00

I’ve created a simple Java project (not a Dynamic Web Project) in Eclipse. I’d

  • 0

I’ve created a simple Java project (not a Dynamic Web Project) in Eclipse.

I’d like to be able to access a web URL that returns some JSON values.

I’m able to get to the URL using HttpClient and all that but I’m unable to Authenticate my session, which is required for me to get access to the JSON data. If you’re not authenticated, the URL redirects you to a login page.

I’ve tried numerous ways of authenticating my session but none seem to work — I always end up up getting login page’s HTML data instead of the JSON.

I’ve also noticed that a JSESSION ID is passed into the Request Header but I can’t seem to figure out how to create a JSESSION ID and how to pass that into the GET request.

So basically, I can solve this issue in two ways:

  1. Authenticate my session and then access the URL to get the JSON
  2. Create a JSESSION ID and pass that into the GET request to get the JSON

Anyone know how to do either?

Eventually, I’d like to use the data I collected from the JSON to do some Selenium testing so do I need to do all this within a Dynamic Web Project (in Eclipse) or can it be all done within a regular Java project?


Attempt 1

URL url = new URL(_url);

InputStream ins = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
StringBuilder builder = new StringBuilder();

for (String line = null; (line = reader.readLine()) != null;) {
    builder.append(line).append("\n");
}

System.out.println("RESPONSE:\n\n" + builder);

JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);

System.out.println("JSON:\n\n" + finalResult.toString());

Attempt 1 Results

I end up printing out the HTML code for the log-in page and then get the following error message since there is no JSON on the page:

Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at character 1

Attempt 2

I have also tried the code found in Oracle’s Http Authentication page:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;

public class RunHttpSpnego {

    static final String kuser = "username"; // your account name
    static final String kpass = "password"; // your password for the account

    static class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            // I haven't checked getRequestingScheme() here, since for NTLM
            // and Negotiate, the usrname and password are all the same.
            System.err.println("Feeding username and password for " + getRequestingScheme());
            return (new PasswordAuthentication(kuser, kpass.toCharArray()));
        }
    }

    public static void main(String[] args) throws Exception {
        Authenticator.setDefault(new MyAuthenticator());
        URL url = new URL(args[0]);
        InputStream ins = url.openConnection().getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
        String str;
        while((str = reader.readLine()) != null)
            System.out.println(str);
    }
}
  • 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-17T09:05:41+00:00Added an answer on June 17, 2026 at 9:05 am

    When you log in, the server sets the session ID as a cookie in your browser using a Set-Cookie header in the response.

    Set-Cookie: name=value
    Set-Cookie: name2=value2; Expires=Wed, 09-Jun-2021 10:18:14 GMT
    

    The browser then provides this cookie to the server with each subsequent request, in the request header.

    Cookie: name=value; name2=value2
    

    HttpClient can do this administration for you, just like the browser. See http://hc.apache.org/httpclient-3.x/cookies.html
    So then you’d first tell HttpClient to surf to the login page, it’ll fetch your cookie and on subsequent requests it’ll send it along.

    You can also take matters into your own hands and set the cookie manually. In HttpClient 3 this looks like this:

    HttpMethod method = new GetMethod();
    method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    method.setRequestHeader("Cookie", "special-cookie=value");
    

    How to set the cookie in HttpClient 4 can be found here: Apache HttpClient 4.0.3 – how do I set cookie with sessionID for POST request

    Or you can take it a little bit lower level and add the full header.

    HttpGet httpget = new HttpGet(url); 
    httpget.addHeader("Cookie", "JSESSIONID=...");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.