im trying to access a webpage and it asks for my email in which it contains a "+" sign .example:"elkowalski+garbage@gmail.com" but instead it tries to write it as "elkowalski garbage@gmail.com" so it fails to log in successfully into the wepage.
i would really appreciate if someone can take me to the right direction or how to fix it. thanks
here is the code:

import java.net.*;
import java.io.*;
public class WebsiteLogin {
    // Variables to hold the URL object and its connection to that URL.
    private static URL URLObj;
    private static URLConnection connect;
    public static void main(String[] args) {
        try {
            // Establish a URL and open a connection to it. Set it to output mode.
            URLObj = new URL("http://example.com/login");
            connect = URLObj.openConnection();
            connect.setDoOutput(true); 
        }
        catch (MalformedURLException ex) {
            System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
            System.exit(1);
        }
        catch (Exception ex) {
            System.out.println("An exception occurred. " + ex.getMessage());
            System.exit(1);
        }
        try {
            // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));

            writer.write("login=elkowalski+garbage@gmail.com&password=123456&submit=Login");
            writer.close();
            // Now establish a buffered reader to read the URLConnection's input stream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            String lineRead = "";

            // Read all available lines of data from the URL and print them to screen.

            while ((lineRead = reader.readLine()) != null) {

                System.out.println(lineRead);

            }



            reader.close();

        }

        catch (Exception ex) {

            System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());

        }

    }

}

+ character has a special meaning when it comes to HTTP POST contents/URL etc. It is decoded to a "space". If you want a literal "plus" sign, you need to encode to something the server side encoder understands. This is where the URLEncoder class comes in. Make sure all the values on the RHS of your equal signs are url encoded with proper encoding.

Something like:

String mail = URLEncoder.encode("a+b@gmail.com", "UTF-8");
String pwd = URLEncoder.encode("yourpassword", "UTF-8");
String action = URLEncoder.encode("Login", "UTF-8");
// now create a POST data string out of these variables and submit them
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.