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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:10:13+00:00 2026-06-16T12:10:13+00:00

I want to login to a website from my Java desktop application. I have

  • 0

I want to login to a website from my Java desktop application. I have to write Username & Password using OutputStreamWriter. I am able to login to other website successfully from my application but not the one which I really want to. After analyzing the page source of that website I found that the ID of those two text boxes for Username & password changes with every request and every time the page refresh.

<input type="text" name="UserName_88515" id="UserName_88515" />

In the id the last 5 digits changes every time, so I decided to read the page source, retrieve those five digits and then write credentials to log in to that website.

public class LoginHandler {

static boolean isLoggedIn = false;
static String responseText, myText;

public void login(String usrname, String password, String cookys, String sessionCode) {

    try {

        URL url = new URL("http://www.somewebsite.com/home.php?session="
                + sessionCode);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0");
        conn.setRequestProperty("Cookie", cookys);
        conn.setDoOutput(true);

        final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        final StringBuilder response = new StringBuilder();
        myText = response.toString();
        String line;

        while ((line = rd.readLine()) != null) {
            response.append(line);
        }
        rd.close();

        final String text1 = response.toString();
        final int starIndex = text1.indexOf("UserName_");
        final int endIndex = starIndex + 15;
        System.err.println("This is starIndex" + starIndex);
        System.err.println("This is endIndex" + endIndex);
        final String avc = text1.substring(starIndex, endIndex);
        System.err.println("This is avc\n" + avc);
        final String fin = avc.substring(10, 15);
        System.err.println("This is fin\n" + fin);

        final String data1 = URLEncoder.encode("MessageLength", "UTF-8")
                + "=" + URLEncoder.encode("140", "UTF-8") + "&"
                + URLEncoder.encode("UserName_" + fin, "UTF-8") + "="
                + URLEncoder.encode("username", "UTF-8") + "&"
                + URLEncoder.encode("Password_" + fin, "UTF-8") + "="
                + URLEncoder.encode("password", "UTF-8") + "&"
                + URLEncoder.encode("LoginNowbtnDiv", "UTF-8") + "="
                + URLEncoder.encode("Login Now", "UTF-8") + "&"
                + URLEncoder.encode("LoginNow", "UTF-8") + "="
                + URLEncoder.encode("Login Now", "UTF-8");

        System.err.println("THIS IS Data1:\n " + data1);

        final OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data1);
        wr.flush();

        final BufferedReader rd1 = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        final StringBuilder response1 = new StringBuilder();
        String line1;

        while ((line1 = rd1.readLine()) != null) {
            response1.append(line1);
        }

        final String text2 = response1.toString();
        System.err.println("This is second response\n" + text2);
        rd1.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}

For that I have written the following code, but I was getting following error:

Cannot write output after reading input
  • 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-16T12:10:15+00:00Added an answer on June 16, 2026 at 12:10 pm

    see the answer to this question – Cannot write output after reading input
    the short version is that the underlying HttpURLConnection (conn) cannot be reused and you’ll need to open a new one.

    generally though, it’ll be easier for yuo if you use a better library for your http code, like HTTPComponents

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

Sidebar

Related Questions

I want to log in to a website from the java desktop application, but
I have a database server which collect user&pass of users (by login from website).
I have a website and use the login using facebook. I want to do
I have an php website with login form and I want every visitors can
I want to implement login using facebook in my windows phone 7.1 application When
I want to create a Login through Facebook on a website using Graph API
I want to fetch all contacts email address from hotmail to my website using
I am creating a website using Java servlets, and I have a page called
I have a problem.. I want to download to XML some info from website
I want to login by using web service. My website is https based. 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.