2020-04-22

[AgentFlow]HttpURLConnection GET and POST

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

public class Customize {
public String sendGET(String url) throws Exception {
URL obj = new URL(url);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("GET");

int responseCode = con.getResponseCode();

if (responseCode <= 209) {
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream(), "UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in .readLine()) != null)
response.append(inputLine);

in .close();

//return response.toString();
String token = "";
String arr[] = response.toString().split(",");
for (int i = 0; i < arr.length; i++) {
if (arr[i].indexOf("\"Token\"") > -1) {
String kvArr[] = arr[i].split(":"); // "Token":"0x4fb0..."
token = kvArr[1]; // "0x4fb0..."
token = token.substring(1, token.length() - 1); // 去雙引號
break;
}
}
return token;
} else {
return "error" + responseCode;
}
}

public String sendPOSTAll(String url, String jsonStr) throws Exception {
URL obj = new URL(url);
//System.out.println(jsonStr);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");

DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(jsonStr.toString().getBytes("UTF-8"));
wr.flush();
wr.close();

int responseCode = con.getResponseCode();

if (responseCode <= 209) {
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream(), "UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in .readLine()) != null)
response.append(inputLine);

in .close();

return response.toString();
} else {
return "sendPOSTAllerror:" + responseCode + con.getResponseMessage();
}
}
}



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;
import java.awt.Desktop;
import java.net.URI;
public class Customize{
public String sendPost(String userName,String personalID,String phone) throws Exception {
String url = "http://127.0.0.1:3390/mine";
URL obj = new URL(url);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");
//con.setRequestProperty("Authorization", "Basic VENHRGlnaXRhbEZvcm06VGFpcGVpMTIzNA==");

String query = "{" +
"\"Username\": \""+ userName +"\"," +
"\"Dept\": \""+ personalID +"\"," +
"\"Email\": \""+ phone +"\"" +
"}";

DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(query.toString().getBytes("UTF-8"));
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
//return Integer.toString(responseCode) +  url + "-v4-" + query ;
if(responseCode <= 209){
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream(),"UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null)
response.append(inputLine);

in.close();

return response.toString();
}
else{
return "error";
}
}
}

沒有留言:

張貼留言