package com.baoxiu.test;

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

/**
 * 发送URL的post请求
 * User: HYY
 * Date: 13-8-16
 * Time: 下午4:44
 * To change this template use File | Settings | File Templates.
 */
public class SendPost {

/**
     * 发送URL的post请求
     *
     * @param urlStr 请求的链接
     * @param params 请求的参数
     */
    public String sendUrl(String urlStr, String params) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuffer result = new StringBuffer();
        try {
            URL realUrl = new URL(urlStr);
            //打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            //这里设置火狐浏览器 V23
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:23.0) Gecko/20100101 Firefox/23.0");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(params);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result.toString();
    }

public static void main(String args[]) {
        String urlStr = "http://reg.email.163.com/unireg/call.do?cmd=added.mobilemail.checkBinding";
        String urlStr2 = "http://reg.email.163.com/unireg/call.do?cmd=register.formLog";
        String urlStr3 = "http://reg.email.163.com/unireg/call.do?cmd=added.mobileverify.sendAcode";
        String params = "mobile=13737461234";
        String params2 = "opt=write_field&flow=mobile&field=mobile&result=done&timecost=6847&level=info";
        String params3 = "mobile=13737461234&uid=13737461234%40163.com&mark=mobile_start";
        new SendPost().sendUrl(urlStr, params);
        new SendPost().sendUrl(urlStr2, params2);
        new SendPost().sendUrl(urlStr3, params3);
    }
}

Java发送post请求的更多相关文章

  1. Java发送Http请求并获取状态码

    通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...

  2. 通过java发送http请求

    通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...

  3. Java发送HTTPS请求

    前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...

  4. 使用Java发送Http请求的内容

    公司要将自己的产品封装一个WebService平台,所以最近开始学习使用Java发送Http请求的内容.这一块之前用PHP的时候写的也比较多,从用最基本的Socket和使用第三方插件都用过. 学习了J ...

  5. Java发送socket请求的工具

    package com.tech.jin.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import ...

  6. Java发送Http请求

    package com.liuyu.test; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  7. 编写爬虫(spider)的预备知识:用java发送HTTP请求

    使用原生API来发送http请求,而不是使用apache的库,原因在于这个第三方库变化实在太快了,每个版本都有不小的变化.对于程序员来说,使用它反而会有很多麻烦,比如自己曾经写过的代码将无法复用. 原 ...

  8. java发送post请求 ,请求数据放到body里

    java利用httpclient发送post请求 ,请求数据放到body里. /** * post请求 ,请求数据放到body里 * * @author lifq * * 2017年3月15日 下午3 ...

  9. JAVA发送HttpClient请求及接收请求结果

    1.写一个HttpRequestUtils工具类,包括post请求和get请求 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...

随机推荐

  1. iOS - 文件操作(File Operating)

    1. 沙盒 & NSData /*_______________________________获取沙盒路径_________________________________________* ...

  2. 博客迁到CSDN

    之前一直是博客园和CSDN博客同步更新 现在不在博客园继续写博客,十一国庆节假后只用CSDN博客了.祝各位访客国庆节快乐! CSDN博客地址: http://blog.csdn.net/it_liuc ...

  3. 引用类型之Array类型

    Array类型 ECMAScript数组与其它语言数组一样,都是数据的有序列表.但是ECMAScript数组的每一项可以保存任何类型的数据.而且,ECMAScript数组是可以动态调整的. 1.创建和 ...

  4. XPOSED-LUA

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/xposed-lua/ 说明 Xposed Lua Module 是一个Xposed的模块,他有下面的优点 本 ...

  5. 在Spring中使用cache(EhCache的对象缓存和页面缓存)

    Spring框架从version3.1开始支持cache,并在version4.1版本中对cache功能进行了增强. spring cache 的关键原理就是 spring AOP,通过 spring ...

  6. jQuery Mobile里xxx怎么用呀?(集成篇)

    jQuery Mobile如何使用GA(Google Analytics)? 什么是GA: http://baike.baidu.com/view/34729.htm http://www.googl ...

  7. [ Database ] [ Sybase ] [ SQLServer ] sybase 與SQL Server的界接方式

    目前我們有個專案Server A安裝了 SQL Server 2012,有個需求需要連線到另外一台Server B上的 Sybase 12.5的view, 先前試過了很多方法都無法連通.主要的原因是因 ...

  8. centos install(160112更新)

    centos安装之后: 更新 yum update 新增用户: useradd myuser passwd myuser 添加sudo: usermod -a -G wheel myuser //vi ...

  9. DICOM开源库

    转载于 http://blog.csdn.net/jackmacro/article/details/5850142 Developers used to search for libraries , ...

  10. win8安装python环境和pip & easy_install工具

    最近在学python,2.7.6的版本 首先安装python2.7 官网下载地址https://www.python.org/downloads/ 下载相应版本即可,应该是一个msi的文件,默认安装到 ...