码迷,mamicode.com
首页 > 编程语言 > 详细

java-http请求

时间:2017-11-14 14:16:51      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:request   eal   tac   gen   []   ace   imp   live   user   

package testHttp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class PostAndGet {

    public static void main(String[] args) {

        sendGet("http://www.baidu.com");
    }

    // Get
    public static void sendGet(String url) {
        BufferedReader bufferedReader = null;
        try {
            URL realurl = new URL(url);
            URLConnection openConnection = realurl.openConnection();
            openConnection.setRequestProperty("accept", "*/*");
            openConnection.setRequestProperty("connection", "Keep-Alive");
            openConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            bufferedReader = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "gbk"));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    // Post
    public static void sendPost(String url, String param) {
        BufferedReader bufferedReader = null;
        try {
            URL realurl = new URL(url);
            URLConnection openConnection = realurl.openConnection();
            openConnection.setRequestProperty("accept", "*/*");
            openConnection.setRequestProperty("connection", "Keep-Alive");
            openConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            openConnection.setDoOutput(true);
            openConnection.setDoInput(true);
            byte[] bytes = param.getBytes();
            openConnection.getOutputStream().write(bytes);
            openConnection.getOutputStream().flush();

            bufferedReader = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "utf-8"));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

 

java-http请求

标签:request   eal   tac   gen   []   ace   imp   live   user   

原文地址:http://www.cnblogs.com/jianxiaopo/p/7831503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!