zoukankan      html  css  js  c++  java
  • JAVA模拟发送HTTP请求

    /*
    * 得到返回的内容
    */
    public static String getResult(String urlStr, String content) {
    URL url = null;
    HttpURLConnection connection = null;

    try {
    url = new URL(urlStr);
    connection = (HttpURLConnection) url.openConnection();//新建连接实例
    connection.setDoOutput(true);//是否打开输出流 true|false
    connection.setDoInput(true);//是否打开输入流true|false
    connection.setRequestMethod("POST");//提交方法POST|GET
    connection.setUseCaches(false);//是否缓存true|false
    connection.connect();//打开连接端口

    DataOutputStream out = new DataOutputStream(connection.getOutputStream());//打开输出流往对端服务器写数据
    out.writeBytes(content);//写数据,也就是提交你的表单 name=xxx&pwd=xxx
    out.flush();//刷新
    out.close();//关闭输出流

    BufferedReader reader = new BufferedReader(new InputStreamReader(connection
    .getInputStream(), "utf-8"));//往对端写完数据 对端服务器返回数据 ,以BufferedReader流来读取
    StringBuffer buffer = new StringBuffer();
    String line = "";
    while ((line = reader.readLine()) != null) {
    buffer.append(line);
    }
    reader.close();
    return buffer.toString();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (connection != null) {
    connection.disconnect();//关闭连接
    }
    }
    return null;
    }


    转载自:http://wenwen.soso.com/z/q183356197.htm

  • 相关阅读:
    人月神话 另外一面
    python论文爬取(五)
    Python词云
    python安装wordcloud库出错及其解决办法(使用命令行安装)
    人月神话 祸起萧墙
    python论文爬取(四)
    个人课程总结
    python论文爬取(三)
    python论文爬取(一)
    win10子系统ubuntu开机启动ssh服务
  • 原文地址:https://www.cnblogs.com/xieyuan/p/3787469.html
Copyright © 2011-2022 走看看