• 客户端发送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();
        }
    
    }
  • 相关阅读:
    数据结构实验之图论六:村村通公路
    数据结构实验之图论二:基于邻接表的广度优先搜索遍历
    数据结构实验之图论二:基于邻接表的广度优先搜索遍历
    树结构练习——判断给定森林中有多少棵树
    树结构练习——判断给定森林中有多少棵树
    图结构练习——最短路径
    图结构练习——最短路径
    【郑轻】[1778]和尚特烦恼4——有多少战斗力
    【郑轻】[1778]和尚特烦恼4——有多少战斗力
    【郑轻】[1776]和尚特烦恼2——第几个素数
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6824857.html
Copyright © 2020-2023  润新知