HttpUrlConnect post提交

Posted aLa神灯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpUrlConnect post提交相关的知识,希望对你有一定的参考价值。

package com.g3.hrp.data_api.Logic;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpPostTest {

    public static void main(String[] args) {
        
//        new HttpPostTest().doJsonPost("http://www.aa.com", "{\"key\":\"value\"}");

    }

    //发送JSON字符串 如果成功则返回成功标识。
    public String doJsonPost(String urlPath, String Json) {
//        System.out.println("Json ----- >>>>"+Json); 
        String result = "";
        HttpURLConnection conn = null;
        InputStream is = null;
        InputStreamReader r =  null;
        BufferedReader reader = null;
        OutputStream outwritestream = null;
        try {
            URL url = new URL(urlPath);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST"); 
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(300000); 
            conn.setDoOutput(true);//向服务器写数据
            conn.setDoInput(true);//读取数据
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
            // 设置接收类型否则返回415错误
            //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型,以此来防范返回415;
            conn.setRequestProperty("accept","application/json");
            
            // 往服务器里面发送数据
            if (Json != null && Json.length()>0) {
                byte[] writebytes = Json.getBytes();
                // 设置文件长度
                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
                outwritestream = conn.getOutputStream();
                outwritestream.write(Json.getBytes());
                outwritestream.flush();
                outwritestream.close();
            }
            conn.connect();
            if (conn.getResponseCode() == 200) {
                is = conn.getInputStream();
                r = new InputStreamReader(is, "utf-8");
                reader = new BufferedReader(r);
//                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                result = reader.readLine();
                System.out.println(result); 
            }
            System.out.println("conn.getResponseCode() ---->>>>"+conn.getResponseCode()); 
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    outwritestream.close();
                    is.close();
                    r.close();
                    reader.close();
                    conn.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    
}

 

以上是关于HttpUrlConnect post提交的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 HttpUrlConnect 使用 java 查询 Github graphql API

表单(上)EasyUI Form 表单EasyUI Validatebox 验证框EasyUI Combobox 组合框EasyUI Combo 组合EasyUI Combotree 组合树((代码片

java.lang.IllegalStateException: Method get not annotated with HTTP method type (ex. GET, POST);(代码片

Android’s HTTP Clients (httpClient 和 httpURLConnect 区别)

Android回顾系列——之HttpUrlConnect的使用

GitGit 分支管理 ( 解决分支合并冲突 | 创建并切换分支 git switch -c feature1 | 修改 feature1 分支并提交 | 修改 master 主版本并提交 )(代码片