java native get and post requests (based java 1.8)

package com.moucong;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;

public class get_post {

/**
* Created by chengxia on 2018/12/4.
*/
public String doPost(String URL){
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
HttpURLConnection conn = null;
try{
URL url = new URL(URL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod ( "POST");
// send a POST request must be set to true
conn.setDoOutput (to true);
conn.setDoInput (to true);
// set the connection time and the read time-out
conn.setConnectTimeout (30000);
conn.setReadTimeout (10000);
conn.setRequestProperty ( "the Content-the Type", "file application / JSON");
conn.setRequestProperty ( "the Accept", "file application / JSON");
// Get the output stream
out = new OutputStreamWriter (conn .getOutputStream ());
// send a request to post json data
String jsonStr = "{\" touser \ ": \" \ ", \" msgtype \ ": \" text \ ", \"text\":{" +
" \"content\":\"\"}}";
out.write(jsonStr);
out.flush();
out.close();
//取得输入流,并使用Reader读取
if (200 == conn.getResponseCode()){
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null){
result.append(line);
System.out.println(line);
}
}else{
System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}catch (IOException ioe){
ioe.printStackTrace();
}
}
return result.toString();
}

public String doGet(String URL){
HttpURLConnection conn = null;
InputStream is = null;
Br = null the BufferedReader;
the StringBuilder the StringBuilder new new Result = ();
the try {
// create a remote connection object url
the URL url = new new the URL (the URL);
// open a connection through a remote connection object url, turn into strong class HTTPURLConnection
conn = ( the HttpURLConnection) url.openConnection ();
conn.setRequestMethod ( "the GET");
// set the connection time and the read time-out
conn.setConnectTimeout (15000);
conn.setReadTimeout (60000);
conn.setRequestProperty ( "the Accept", "file application / JSON");
// send a request
conn.connect ();
// input stream acquired by conn, read and use the Reader
if (200 == conn.getResponseCode()){
is = conn.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while ((line = br.readLine()) != null){
result.append(line);
// System.out.println(line);
}
//json类型字符串转为jsonObject
// System.out.println(result);
JSONObject jsonObject = JSONObject.parseObject(String.valueOf(result));
//根据key值获取数据value
System.out.println(jsonObject.getString("access_token")); //输出value1
Writer writer = null;
StringBuilder outputString = new StringBuilder();
try {
outputString.append(jsonObject.getString("access_token") + "\r\n");
writer = new FileWriter("test.txt"); // true表示追加
writer.write(outputString.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}



}else{
System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
}
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(br != null){
br.close();
}
if(is != null){
is.close ();
}
} the catch (IOException IOE) {
ioe.printStackTrace ();
}
conn.disconnect ();
}
return result.toString ();
}


/ ** reads file data stored in the set
* @ * return /
public static String readtFile (File File) throws IOException, {a ParseException
the InputStreamReader Read = null; // encoding format into consideration
the try {
Read the InputStreamReader new new = (new new the FileInputStream (File), "UTF-. 8");
} the catch ( E UnsupportedEncodingException) {
e.printStackTrace ();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
StringBuffer buffer = new StringBuffer();
String line = " ";
while ((line = bufferedReader.readLine()) != null) {
// System.out.println(lineTxt);
buffer.append(line);
}

read.close();
return buffer.toString();
}


public static void main(String[] args) throws Exception {
get_post http = new get_post();

// System.out.println("Testing 1 - Do Http GET request");
//获取token的get方法
// http.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=");

System.out.println("\nTesting 2 - Do Http POST request");
File file = new File("test.txt");
String token = readtFile(file);
System.out.println(token);
http.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+token);

}

}

Guess you like

Origin www.cnblogs.com/setname/p/11964815.html
Recommended