Search code examples
javastringstringbuilder

Putting 2 Strings Together to Build URL with ResultSet


I'm trying to build a url from 2 parts of my result set. The Prop_Value itself is a URL alone, but I need to add a Ven_Item to the end of it. Here is my code involving the build:

String first = (rs1.getString("PROP_VALUE"));   
String second = (rs1.getString("VEN_ITEM"));

String url = (first+second);

URL obj = new URL(url);

HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

int  responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " +url);
System.out.println("Response Code : " + responseCode);


BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    content.append(inputLine);
    }
    in.close();
    con.disconnect(); 

It will run through, but it is only sending over the PROP_VALUE and not attaching the VEN_ITEM to it, any suggestions?


Solution

  • I think something wrong with your first and second variable values. Please check. Bcoz, below code is running fine.

        String first = ("https://stackoverflow.com/questions");
        String second = ("/48545905/putting-2-strings-together-to-build-url-with-resultset");
    
        String url = (first+second);
    
        URL obj = null;
        try {
            obj = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    
        HttpsURLConnection con = null;
        try {
            con = (HttpsURLConnection) obj.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        int  responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " +url);
        System.out.println("Response Code : " + responseCode);
    
    
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer content = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        System.out.println(content);
        in.close();
        con.disconnect();
    

    Got the Below console output:

    Sending 'GET' request to URL : Putting 2 Strings Together to Build URL with ResultSet Response Code : 200