java请求数据并存入数据库 Posted on 2019-03-04 | In 亿信华辰 | 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179package com.esen.gxdc.test;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import javax.net.ssl.HttpsURLConnection;import org.drools.lang.DRLParser.neg_operator_key_return;import org.json.JSONArray;import org.json.JSONObject;import com.esen.gxdc.dao.ApiBikeInfoAllDao;import com.esen.gxdc.dao.ApiBikeInfoDao;import com.esen.gxdc.entity.ApiBikeInfo;import com.google.gson.JsonArray;import com.sun.xml.bind.unmarshaller.InfosetScanner;public class ApiInfo2db { private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { ApiInfo2db http = new ApiInfo2db();// System.out.println("Testing 1 - Send Http GET request"); http.sendGetBike(); http.sendGetCompany();// System.out.println("\nTesting 2 - Send Http POST request");// http.sendPost(); } // HTTP GET请求 private void sendGetBike() throws Exception { String url = "http://localhost:8888/ssm/getApiData.do"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //默认值我GET con.setRequestMethod("GET"); //添加请求头 con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //打印结果 System.out.println(response.toString()); JSONArray jsonArray=new JSONArray(response.toString()); List<ApiBikeInfo>infos=new ArrayList<ApiBikeInfo>(); for(int i=0;i<jsonArray.length();i++){ ApiBikeInfo apiBikeInfo=new ApiBikeInfo(); apiBikeInfo.setId(jsonArray.getJSONObject(i).getString("Id")); apiBikeInfo.setCompanyid(jsonArray.getJSONObject(i).getString("CompanyId")); apiBikeInfo.setCode(jsonArray.getJSONObject(i).getString("Code")); apiBikeInfo.setLicense(jsonArray.getJSONObject(i).getString("License")); apiBikeInfo.setLaunchdate(jsonArray.getJSONObject(i).getString("LaunchDate")); apiBikeInfo.setState(jsonArray.getJSONObject(i).getString("State")); apiBikeInfo.setUpdatetime(jsonArray.getJSONObject(i).getString("UpdateTime")); apiBikeInfo.setUploadtime(jsonArray.getJSONObject(i).getString("UploadTime")); apiBikeInfo.setHistory(jsonArray.getJSONObject(i).getString("History")); infos.add(apiBikeInfo); ApiBikeInfoDao.getInstance().add(apiBikeInfo); } } // HTTP GET请求 private void sendGetCompany() throws Exception { String url = "http://localhost:8888/ssm/getApiData.do"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //默认值我GET con.setRequestMethod("GET"); //添加请求头 con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //打印结果 System.out.println(response.toString()); JSONArray jsonArray=new JSONArray(response.toString()); List<ApiBikeInfo>infos=new ArrayList<ApiBikeInfo>(); for(int i=0;i<jsonArray.length();i++){ ApiBikeInfo apiBikeInfo=new ApiBikeInfo(); apiBikeInfo.setId(jsonArray.getJSONObject(i).getString("Id")); apiBikeInfo.setCompanyid(jsonArray.getJSONObject(i).getString("CompanyId")); apiBikeInfo.setCode(jsonArray.getJSONObject(i).getString("Code")); apiBikeInfo.setLicense(jsonArray.getJSONObject(i).getString("License")); apiBikeInfo.setLaunchdate(jsonArray.getJSONObject(i).getString("LaunchDate")); apiBikeInfo.setState(jsonArray.getJSONObject(i).getString("State")); apiBikeInfo.setUpdatetime(jsonArray.getJSONObject(i).getString("UpdateTime")); apiBikeInfo.setUploadtime(jsonArray.getJSONObject(i).getString("UploadTime")); apiBikeInfo.setHistory(jsonArray.getJSONObject(i).getString("History")); infos.add(apiBikeInfo); ApiBikeInfoDao.getInstance().add(apiBikeInfo); } } // HTTP POST请求 private void sendPost() throws Exception { String url = "https://selfsolve.apple.com/wcResults.do"; URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); //添加请求头 con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"; //发送Post请求 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //打印结果 System.out.println(response.toString()); }} `xml