<pre name="code" class="java">//获取http响应
public static void read() throws IOException {
URL url = new URL("http://localhost:8080/ServletDemo1");
HttpURLConnection content = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(content.getInputStream()));
String s = br.readLine();
String server = content.getHeaderField("Server");
int responseCode = content.getResponseCode();
System.out.println(s);
System.out.println(server);
System.out.println(responseCode);
}
写数据,getResponse代表请求结束!
//提交http请求给服务器
public static void write() throws IOException {
URL url =new URL("http://localhost:8080/ServletDemo1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//写请求方法
conn.setRequestMethod("post");
//写header
conn.setRequestProperty("xxx","yyy");
//写数据
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write("name=flx".getBytes());
conn.getResponseCode();
}