zoukankan      html  css  js  c++  java
  • 客户端发送http

    package com.scok;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    
    
    
    public class Test {
        /**
         * Main
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception {
            System.out.println(doPost());
        }
        
        /**
         * Post Request
         * @return
         * @throws Exception
         */
        public static String doPost() throws Exception {
            String parameterData = "eventType=1&username=nickhuang&version=1.1";
            
            URL localURL = new URL("http://localhost:8080/lbs-service-api/lbs/api/shipment.do");
            URLConnection connection = localURL.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
            
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));
            
            OutputStream outputStream = null;
            OutputStreamWriter outputStreamWriter = null;
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null;
            BufferedReader reader = null;
            StringBuffer resultBuffer = new StringBuffer();
            String tempLine = null;
            
            try {
                outputStream = httpURLConnection.getOutputStream();
                outputStreamWriter = new OutputStreamWriter(outputStream);
                
                outputStreamWriter.write(parameterData.toString());
                outputStreamWriter.flush();
                
                if (httpURLConnection.getResponseCode() >= 300) {
                    throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
                }
                
                inputStream = httpURLConnection.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                reader = new BufferedReader(inputStreamReader);
                
                while ((tempLine = reader.readLine()) != null) {
                    resultBuffer.append(tempLine);
                }
                
            } finally {
                
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }
                
                if (outputStream != null) {
                    outputStream.close();
                }
                
                if (reader != null) {
                    reader.close();
                }
                
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                
                if (inputStream != null) {
                    inputStream.close();
                }
                
            }
    
            return resultBuffer.toString();
        }
    
    }
  • 相关阅读:
    【php】PHP检测json格式数据
    【php】PHP那些非常有用却鲜有人知的函数
    MySQL DELETE 语句:语法及案例剖析、从命令行中删除数据
    MySQL UPDATE 更新:语法及案例剖析
    MySQL WHERE 子句:语法及案例剖析、从命令提示符中读取数据
    mysql实现主从复制/主从同步
    docker安装mysql方法
    MySQL之账户管理的几种方式
    MySQL 查询数据:语法及案例剖析
    MySQL 插入数据:语法以及案例剖析
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6824857.html
Copyright © 2011-2022 走看看