java post request code

/*
 * Project Name:Tconfig
 * File Name:Main.java
 * Package Name:packages
 * Date:2017-3-21下午6:03:01
 * Copyright (c) 2017, chinhiyoshi@163.com 陈飞  Rights Reserved.
 *
 */
package packages;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import common.Common;
import data.S1;

/*
 * ClassName:Main  
 * Function:   ADD FUNCTION.  
 * Reason:     ADD REASON.  
 * Date:     2017-3-21 下午6:03:01  
 * @author   guosen
 * @version   
 * @since    JDK 1.6   
 */
public class Main {

    public static void main(String[] args) throws InterruptedException,IOException {

        //接口地址
        String urlStr = "http://172.24.175.81:8081/WebServices/Seal.ashx";
        
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);  
        conn.setDoInput(true);  
        conn.setRequestMethod("POST"); // POST方法
        conn.connect();
        
        // 写入的POST数据
        OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
        osw.write(S1.jsonString);
        osw.flush();
        osw.close();
        
        // 读取响应数据
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String s;
        while ((s = in.readLine()) != null)
            System.out.println(s);

        /*
         * while(true) { System.out.println(new Common().getsecnum());
         * Thread.sleep(1000); }
         */

    }
}

 

posted @ 2017-03-30 15:42 araki 阅读(...) 评论(...) 编辑 收藏