2016-07-01 11 views
-3
 URL aURL = new URL("http://stackoverflow.com/"); 

    System.out.println("protocol = " + aURL.getProtocol()); 
    System.out.println("authority = " + aURL.getAuthority()); 
    System.out.println("host = " + aURL.getHost()); 
    System.out.println("port = " + aURL.getPort()); 
    System.out.println("path = " + aURL.getPath()); 
    System.out.println("query = " + aURL.getQuery()); 
    System.out.println("filename = " + aURL.getFile()); 
    System.out.println("ref = " + aURL.getRef()); 

上記のコードはURLのいくつかのプロパティを示していますが、URLで実行されたクエリの結果を取得する方法があるかどうかを知る必要がありますテスト目的のためのウェブサイトを持っていると私はURLを介してDBのクエリを作成している(主にSELECT)とWebページのHTMLで結果を表示している、私はそれまで、特定のSELECTの結果を取得する機能があるのだろうか? .txtにページのHTMLをダウンロードし、コンテンツをソートしてからクエリの結果を取得する方法ですが、あまり実用的ではありません。 -Regardsjava:URLクエリの結果を取得する方法

+2

httpリクエストの作成方法を尋ねていますか? –

+1

...コード内のSQLデータベースを直接照会する方法を質問しています([JDBC](https://docs.oracle.com/javase/tutorial/jdbc/)を参照) – copeg

+0

idealy、私は取得したいURLを介した文字列へのSQLクエリ結果 – irregular

答えて

0

明らかに単純なgetを実行します。 ソース:http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

private void sendGet() throws Exception { 

    String url = "your url here"; 

    URL obj = new URL(url); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

    // optional default is GET 
    con.setRequestMethod("GET"); 

    //add request header 
    con.setRequestProperty("User-Agent", USER_AGENT); 

    int responseCode = con.getResponseCode(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
    String inputLine; 
    StringBuffer response = new StringBuffer(); 

    while ((inputLine = in.readLine()) != null) { 
     response.append(inputLine); 
    } 
    in.close(); 
    System.out.println(response.toString()); 

} 
関連する問題