java模拟http请求携带header调用腾讯企业邮箱开放接口

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.dupeng.https.HttpRequest;

public class PostData {
	
	public static void main(String[] args){
		String token =args[0];
		String sr=HttpRequest.sendPost("https://tel.exmail.qq.com/cgi-bin/token", "grant_type=client_credentials&client_id=bizmx&client_secret="+token);
		JSONObject jsonObj = JSONObject.fromObject(sr);
	    String s = (String) jsonObj.get("access_token");
	    doPost(s);
	}
	
	public static void doPost(String s){
		final String website = "http://openapi.exmail.qq.com:12211";
        final int connectTimeOut = 15000;
        final int readDataTimeOut = 50000;
        HttpURLConnection httpConn = null;
        String htmlContent = null;
        String requestCookie = null;
        String reqUrl = "/openapi/user/sync";
        try{
            // 第一次请求【POST】
            // 1、创建连接
            URL url = new URL(website + reqUrl);
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            httpConn.setUseCaches(false);
            httpConn.setConnectTimeout(connectTimeOut);
            httpConn.setReadTimeout(readDataTimeOut);
            // 2、设置请求头
            httpConn.setRequestProperty("Authorization"," Bearer "+s);
            // 3、连接
            httpConn.setRequestMethod("POST");
            httpConn.connect();
            
            // 4、设置请求参数
            OutputStream outStream = httpConn.getOutputStream();
            String postData = "[email protected]&Password=1qaz3wsx&Md5=0";
            outStream.write(postData.getBytes());
            outStream.flush();
            outStream.close();
            
            // 5、获取响应结果
            // 获取响应头信息
            Map> resHeaderMap = httpConn.getHeaderFields();
            if (null != resHeaderMap && false == resHeaderMap.isEmpty()){
                for (Map.Entry> entry : resHeaderMap.entrySet()){
                    String key = entry.getKey();
                    String value = java.util.Arrays.toString(entry.getValue().toArray());
                    if (null != key && "Set-Cookie".equals(key.trim())){
                        requestCookie = value;
                        requestCookie = requestCookie.replace("[", "");
                        requestCookie = requestCookie.replace("]", "");
                    }
                }
            }
            if (HttpURLConnection.HTTP_OK == httpConn.getResponseCode()){
                InputStream inStream = httpConn.getInputStream();
                htmlContent = inputStream2String(inStream);
                System.out.println(htmlContent);
            }
		} catch (IOException e){
		    e.printStackTrace();
		} finally{
		    if (null != httpConn){
		        try{
		        	httpConn.disconnect();
		        } catch (Exception e){
		        }
		    }
		}
	}
	
	public static String inputStream2String(InputStream is) throws IOException{ 
		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		int i=-1; 
		while((i=is.read())!=-1){ 
			baos.write(i); 
		} 
       return baos.toString(); 
	}
}

你可能感兴趣的:(技术学习,http,java,header)