zoukankan      html  css  js  c++  java
  • java HTTP基本框架


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;

    public class LoginUtil {

    public static String Get(String url,String key) {
    {
    String proxyHost="";
    int proxyPort=111;
    boolean isproxy=false;
    String result = "";
    BufferedReader in = null;
    try {
    URL realUrl = new URL(url);
    HttpURLConnection conn = null;
    if (isproxy) {
    //使用代理IP
    @SuppressWarnings("static-access")
    Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    conn = (HttpURLConnection) realUrl.openConnection(proxy); //使用代理IP,打开和URL之间的连接
    }else {
    //不使用代理IP
    conn = (HttpURLConnection) realUrl.openConnection(); //不使用代理IP,打开和URL之间的连接
    }
    // 发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    // 设置通用的请求属性
    conn.setConnectTimeout(2 * 1000);
    conn.setRequestProperty("accept", "*/*");
    conn.setRequestProperty("connection", "Keep-Alive");
    conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    //conn.setRequestProperty("Referer", referer);
    conn.setRequestProperty("key", key);
    // 建立真实HTTP连接
    conn.connect();
    // 定义BufferedReader输入流来读取URL的响应
    in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
    String line;
    while ((line = in.readLine()) != null) {
    result = result + line + " ";
    }
    } catch (Exception e) {
    System.out.println("发送 GET 请求出现异常!" + e);
    e.printStackTrace();
    } finally {
    try{
    if(in!=null){
    in.close();
    }
    }
    catch(IOException ex){
    ex.printStackTrace();
    }
    }
    return result;
    }
    }
    public static String Sendgift(String url, String param,String key) {
    String result = "";
    int proxyPort=111;
    String proxyHost="1.1.1.1";
    boolean isproxy=false;
    OutputStreamWriter out = null;
    BufferedReader in = null;
    try {
    URL realUrl = new URL(url);
    HttpURLConnection conn = null;
    if (isproxy) {
    //使用代理IP
    @SuppressWarnings("static-access")
    Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    conn = (HttpURLConnection) realUrl.openConnection(proxy); //使用代理IP,打开和URL之间的连接
    }else {
    //不使用代理IP
    conn = (HttpURLConnection) realUrl.openConnection(); //不使用代理IP,打开和URL之间的连接
    }
    // 发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    // POST方法
    conn.setRequestMethod("POST");
    // 设置通用的请求属性
    conn.setConnectTimeout(5 * 1000);
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36");
    conn.setRequestProperty("Host", "www.sjlive.cn");
    conn.setRequestProperty("key", key);
    // 建立真实HTTP连接
    conn.connect();
    // 获取URLConnection对象对应的输出流
    out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
    // 发送请求参数
    out.write(param);
    // flush输出流的缓冲
    out.flush();
    // 定义BufferedReader输入流来读取URL的响应
    in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
    String line;
    while ((line = in.readLine()) != null) {
    result = result + line + " ";
    }
    } catch (Exception e) {
    System.out.println("发送 POST 请求出现异常!" + e);
    e.printStackTrace();
    } finally {
    try{
    if(out != null){
    out.close();
    }
    if(in != null){
    in.close();
    }
    }
    catch(IOException ex){
    ex.printStackTrace();
    }
    }
    return result;
    }
    }

  • 相关阅读:
    Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
    Spring Boot 如何给微信公众号返回消息
    Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
    Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
    Spring Boot 开发微信公众号后台
    Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
    Spring Boot2 系列教程(十六)定时任务的两种实现方式
    Spring Boot2 系列教程(十五)定义系统启动任务的两种方式
    Spring Boot2 系列教程(十四)CORS 解决跨域问题
    JavaScript二维数组
  • 原文地址:https://www.cnblogs.com/tester-huang/p/5531916.html
Copyright © 2011-2022 走看看