Java tutorial
package bigbluej; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author Michael Lieshoff */ public class Crawler { private HttpClientFactory httpClientFactory = new HttpClientFactory(); public String post(String url) throws IOException { System.out.println("post> " + url); HttpClient client = httpClientFactory.create(); HttpPost request = new HttpPost(url); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder s = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { s.append(line); } return s.toString(); } public String post(String url, String body) throws IOException { System.out.println("post with body> " + url); HttpClient client = httpClientFactory.create(); HttpPost request = new HttpPost(url); request.setEntity(new StringEntity(body)); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder s = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { s.append(line); } return s.toString(); } public String get(String url) throws IOException { System.out.println("get> " + url); HttpClient client = httpClientFactory.create(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder s = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { s.append(line); } return s.toString(); } }