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("UTF-8");
                // 设置文件长度
                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
                outwritestream = conn.getOutputStream();
                outwritestream.write(writebytes);
                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;
    }
    
}
向Internet发送请求参数
步骤:


2)创建URL对象:URL realUrl = new URL(requestUrl);

3)通过HttpURLConnection对象,向网络地址发送请求:HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();

4)设置容许输出:conn.setDoOutput(true);

5)设置不使用缓存:conn.setUseCaches(false);
6)设置使用POST的方式发送:conn.setRequestMethod("POST");
           
7)设置维持长连接:conn.setRequestProperty("Connection", "Keep-Alive");

8)设置文件字符集:conn.setRequestProperty("Charset", "UTF-8");

9)设置文件长度:conn.setRequestProperty("Content-Length", String.valueOf(data.length));

10)设置文件类型:conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

11)以流的方式输出.

总结:
--发送POST请求必须设置允许输出
--不要使用缓存,容易出现问题.
--在开始用HttpURLConnection对象的setRequestProperty()设置,就是生成HTML文件头.
4.向Internet发送xml数据
XML格式是通信的标准语言,Android系统也可以通过发送XML文件传输数据.
步骤:
1)将生成的XML文件写入到byte数组中,并设置为UTF-8:byte[] xmlbyte = xml.toString().getBytes("UTF-8");
2)创建URL对象,并指定地址和参数:URL url = new URL(http://localhost:8080/itcast/contanctmanage.do?method=readxml );
3)获得链接:HttpURLConnection conn = (HttpURLConnection) url.openConnection();
4)设置连接超时:conn.setConnectTimeout(6* 1000);
5)设置允许输出conn.setDoOutput(true);
6)设置不使用缓存:conn.setUseCaches(false);
7)设置以POST方式传输:conn.setRequestMethod("POST");           
8)维持长连接:conn.setRequestProperty("Connection", "Keep-Alive");
9)设置字符集:conn.setRequestProperty("Charset", "UTF-8");
10)设置文件的总长度:conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length));
11)设置文件类型:conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
12)以文件流的方式发送xml数据:outStream.write(xmlbyte);
总结:

--我们使用的是用HTML的方式传输文件,这个方式只能传输一般在5M一下的文件.
--传输大文件不适合用HTML的方式,传输大文件我们要面向Socket编程.确保程序的稳定性
1)将地址和参数存到byte数组中:byte[] data = params.toString().getBytes(); 
原文地址:https://www.cnblogs.com/lxh520/p/8421860.html