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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:32:35+00:00 2026-05-23T02:32:35+00:00

I’m trying to make a Java application that sends some POST requests to a

  • 0

I’m trying to make a Java application that sends some POST requests to a server. The first request is the one with authentication information. Then when I send the next request, I’m getting the answer that my session is expired. But I’m sending the next request within the same second, so, it can’t be timed out.

So I guess there is something like a HTTP Session in Java, which I need to use for sending my request that way the server knows its following the previous request.

I’ve been searching Google, but can’t find anything. Only something about Servlets. But I’m creating a desktop application.

PS: I’m new to sending HTTP requests and this kind of things.

Thanks in advance,
Martijn


Edit: This is the code I use currently:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author martijn
 */
public class PostRequest {

    private String _url;
    private List<PostParameter> params;

    public PostRequest(String url) {
        this._url = url;
        this.params = new ArrayList<PostParameter>();
    }

    public void addParam(String key, String value)
    {
        params.add(new PostParameter(key, value));
    }

    public String getURL()
    {
        return _url;
    }

    public InputStream request() throws IOException {
        URL url = new URL(this._url);
        URLConnection conn = url.openConnection();
        if (params.size() > 0) {

            conn.setDoOutput(true);

            StringBuilder data = new StringBuilder();
            for (int i = 0; i < params.size(); i++) {
                if (i > 0) {
                    data.append("&");
                }
                String key = params.get(i).key;
                String value = params.get(i).value;
                data.append(URLEncoder.encode(key, "UTF-8"));
                data.append("=");
                data.append(URLEncoder.encode(value, "UTF-8"));
            }

            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data.toString());
            wr.flush();
        }

        return conn.getInputStream();
    }

    public class PostParameter {

        public String key;
        public String value;

        public PostParameter(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public PostParameter() {
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}

And then send two request after each other:

PostRequest postAuthentication = new PostRequest("http://blahblah.com");
postAuthentication.addParam("user", user);
postAuthentication.addParam("password", pass);
Utils.dumpReader(postAuthentication.request());

PostRequest postDoSomething = new PostRequest("http://blahblah.com/function.php");
postDoSomething.addParam("func", "blah");
postDoSomething.addParam("value", "14");
Utils.dumpReader(postDoSomething.request());
    // Here I'm getting the session is expired.
  • 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-23T02:32:35+00:00Added an answer on May 23, 2026 at 2:32 am

    These will help you:

    • Apache HttpComponents
    • Apache HttpClient
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.