您的位置:首页 > 理论基础 > 计算机网络

一个手写的 http client

2015-07-16 10:30 489 查看
public class HTTPClient {

public static final String GET = "GET";
public static final String POST = "POST";
public static final String PUT = "PUT";
public static final String DELETE = "DELETE";

public static String getData(String path) throws Exception {
String responseData = visitWithoutParam(path, GET);
return responseData;
}

public static String postData(String path, String data) throws Exception {
String responseData = visitWithParam(path, POST, data);
return responseData;
}

public static String putData(String path, String data) throws Exception {
return "To do put data";
}

public static String deleteData(String path) throws Exception {
return "To do delete data";
}

public static String visitWithParam(String path, String method, String body) throws Exception{
InputStream inputStream = null;
BufferedReader bufferedReader = null;

try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);

httpURLConnection.setRequestMethod(method);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Length", Integer.toString(body.getBytes().length));

DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.writeBytes(body);
dataOutputStream.flush();
dataOutputStream.close();

inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;

while((line = bufferedReader.readLine()) != null)
stringBuilder.append(line);

return stringBuilder.toString();
} catch (Exception e) {
throw new Exception(e);
} finally {
// no need to change null actually
try {
if(bufferedReader != null) bufferedReader.close();
if(inputStream != null) inputStream.close();
} catch (Exception e){

}
}
}

public static String visitWithoutParam(String path, String method) throws Exception {
InputStream inputStream = null;
BufferedReader bufferedReader = null;

URL url;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);

httpURLConnection.setRequestMethod(method);

inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

StringBuilder stringBuilder = new StringBuilder();
String line = null;

while((line = bufferedReader.readLine()) != null)
stringBuilder.append(line);

return stringBuilder.toString();
} catch (Exception e) {
throw new Exception(e);
} finally {
try {
if(bufferedReader != null) bufferedReader.close();
if(inputStream != null) inputStream.close();
} catch (Exception e) {

}
}

}
}


自己很久以前写过的一段代码,当时忘记了 apache.httpclient 这个东西,结果又重新造了个轮子  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: