基本HTTP请求回顾

HttpURLConnection为javaAPI提供的一种Rest访问的方式。其支持对Post,Delete,Get,Put等方式的访问。

以下为对于HttpURLConnection对Post等方式访问的一段代码。

view plaincopy to clipboardprint?

  package com.jago.cdn.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class URLConnectionUtil {
    
     private static final String SERVLET_POST = "POST" ;
     private static final String SERVLET_GET = "GET" ;
     private static final String SERVLET_DELETE = "DELETE" ;
     private static final String SERVLET_PUT = "PUT" ;
     
     private static String prepareParam(Map<String,Object> paramMap){
         StringBuffer sb = new StringBuffer();
         if(paramMap.isEmpty()){
             return "" ;
         }else{
             for(String key: paramMap.keySet()){
                 String value = (String)paramMap.get(key);
                 if(sb.length()<1){
                     sb.append(key).append("=").append(value);
                 }else{
                     sb.append("&").append(key).append("=").append(value);
                 }
             }
             return sb.toString();
         }
     }
     
     public static void  doPost(String urlStr,Map<String,Object> paramMap ) throws Exception{
         URL url = new URL(urlStr);
         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
         conn.setRequestMethod(SERVLET_POST);
         String paramStr = prepareParam(paramMap);
         conn.setDoInput(true);
         conn.setDoOutput(true);
         OutputStream os = conn.getOutputStream();     
         os.write(paramStr.toString().getBytes("utf-8"));     
         os.close();         
         
         BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         String line ;
         String result ="";
         while( (line =br.readLine()) != null ){
             result += "/n"+line;
         }
         System.out.println(result);
         br.close();
         
     }
     
     public static void  doGet(String urlStr,Map<String,Object> paramMap ) throws Exception{
         String paramStr = prepareParam(paramMap);
         if(paramStr == null || paramStr.trim().length()<1){
             
         }else{
             urlStr +="?"+paramStr;
         }
         System.out.println(urlStr);
         URL url = new URL(urlStr);
         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
         conn.setRequestMethod(SERVLET_GET);
         conn.setRequestProperty("Content-Type","text/html; charset=UTF-8");
         
         conn.connect();
         BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         String line ;
         String result ="";
         while( (line =br.readLine()) != null ){
             result += "/n"+line;
         }
         System.out.println(result);
         br.close();
     }
     
     public static void doPut(String urlStr,Map<String,Object> paramMap) throws Exception{
         URL url = new URL(urlStr);
         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
         conn.setRequestMethod(SERVLET_PUT);
         String paramStr = prepareParam(paramMap);
         conn.setDoInput(true);
         conn.setDoOutput(true);
         OutputStream os = conn.getOutputStream();     
         os.write(paramStr.toString().getBytes("utf-8"));     
         os.close();         
         
         BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         String line ;
         String result ="";
         while( (line =br.readLine()) != null ){
             result += "/n"+line;
         }
         System.out.println(result);
         br.close();
                 
     }
     
     public static void doDelete(String urlStr,Map<String,Object> paramMap) throws Exception{
         String paramStr = prepareParam(paramMap);
         if(paramStr == null || paramStr.trim().length()<1){
             
         }else{
             urlStr +="?"+paramStr;
         }   
         System.out.println(urlStr);
         URL url = new URL(urlStr);
         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
         conn.setDoOutput(true);
         conn.setRequestMethod(SERVLET_DELETE);
         if(conn.getResponseCode() ==200){
             System.out.println("成功");
         }else{
             System.out.println(conn.getResponseCode());
         }
     }
     
     public static void main(String[] args) throws Exception{
         String urlStr = "http://localhost:8080/SwTest/ReceiveDataServlet";
         Map<String,Object> map = new HashMap<String,Object>();
         map.put("username","张三");
         map.put("password","88888");
 //      URLConnectionUtil.doGet(urlStr, map);
 //      URLConnectionUtil.doPost(urlStr, map);
 //      URLConnectionUtil.doPut(urlStr, map);
         URLConnectionUtil.doDelete(urlStr, map);
         
     }
     
     
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值