有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将2个字符串放在一起以使用ResultSet构建URL

我正在尝试从结果集中的两部分构建url。Prop_值本身只是一个URL,但我需要在它的末尾添加一个venu项。以下是我涉及构建的代码:

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(); 

它将运行,但它只发送PROP_值,而不将venu项附加到它,有什么建议吗


共 (1) 个答案

  1. # 1 楼答案

    我认为第一个和第二个变量值有问题。请查收。Bcoz,下面的代码运行良好

        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();
    

    已获得以下控制台输出:

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