http://blog.csdn.net/zhliro/article/details/46877519
1、HttpUrlConnection 有get请求 post请求
URL url=new Url(url);
HttpUrlConnection httpUrlconnection=url.oprn.Connection();
httpUrlConnection.setRequestMethod("GET"); get请求,需要大写
httpUrlConnection.setRequestMethod("POST"); post请求
post请求需要配置这两个方法
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
post传值
String post="cardNum=123456";
PrintWriter writer=new PrintWriter(httpConnection.getOutputStream());
writer.write(post);
writer.flush();
返回值输出
BufferedInputStream bis=new BufferedInputStream(httpConnection.getInputStream());
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int len;
byte[] arr=new byte[1024];
while((len=bis.read(arr))!=-1){
bos.write(arr, 0, len);
bos.flush();
}
bos.close();
System.out.println(bos.toString("utf-8"));
get方式返回值输出
connection.connect();
// 4. 得到响应状态码的返回值 responseCode
int code = connection.getResponseCode();
String msg = "";
System.out.println(code);
if (code == 200) { // 正常响应
// 从流中读取响应信息
System.out.println("================");
InputStream is =connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while(-1 != (len = is.read(buffer))){
baos.write(buffer,0,len);
baos.flush();
}
System.out.println(baos.toString("utf-8"));
}
// 6. 断开连接,释放资源
connection.disconnect();
Httpclient get post 请求
public static void httpClientPostTest() {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(
"http://localhost:8080/TkdhallCert/examQuestionController/judge/2c9086e15dcb18f8015dcb271a1a0000/submitPaper.do");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("cardNum", "123456"));
try {
// import org.apache.http.NameValuePair;
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("响应内容:");
System.out.println(EntityUtils.toString(entity));
}
response.close();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void httpClientGetTest() {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet=new HttpGet("http://localhost:8080/TkdhallCert/examQuestionController/judge/2c9086e15dcb18f8015dcb271a1a0000/submitPaper.do?cardNum=123456");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
System.out.println(response.getStatusLine());
HttpEntity entity=response.getEntity();
if(entity!=null){
System.out.println("长度"+entity.getContentLength());
System.out.println("内容"+EntityUtils.toString(entity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}