MinsungLee
MinsungLee

Reputation: 11

api layer, exchangerate // IOException : Server returned HTTP response code: 403 for URL

control.java

public class control {

    public static void main(String[] args) {
        boolean success = true;
        String timestamp = "1659072666";
        String base = "USD";
        String date = "2022-07-29";
        double GBP = 0.82011;
        double JPY = 133.000499;
        double EUR = 0.979105;

        
        excJSON vwJson = new excJSON();

        exc vw = vwJson.getexc(success, timestamp, base, date, GBP, JPY, EUR);

        excDAO vwDao = new excDAO();
        vwDao.intertexc(1, vw);
    }
}

exc.java

public class exc {
    String base;
    String date;
    String timestamp;

    public String getbase() {
        return base;
    }
    public void setbase(String base) {
        this.base = base;
    }
    public String getdate() {
        return date;
    }
    public void setdate(String date) {
        this.date = date;
    }
    public String gettimestamp() {
        return timestamp;
    }
    public void settimestamp(String timestamp) {
        this.timestamp = timestamp;
    }
}

excDAO.java

import java.sql.Statement;
import java.sql.Connection;
//import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.util.Enumeration;

public class excDAO {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/test?useSSL=false"; 
    static final String USERNAME = "root"; // DB ID
    static final String PASSWORD = "************"; // DB Password

    private Connection conn = null;
    private Statement stmt = null;
    private ResultSet rs = null;

    public void intertVillageWeather(int id, exc v) {
        
        String query = "INSERT INTO exchangerate"
                + " VALUE(" + id +",'"+v.gettimestamp() + "','" + v.getbase() + "','" + v.getdate() + "');";
        System.out.print("Your Database in : ");
        try {
        
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD); 
            
            
            if (conn != null){System.out.println("good");}
            else{System.out.println("bad");}
            
            System.out.println(query); 
            
            stmt = conn.createStatement(); 
            stmt.executeUpdate(query);
            stmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Exection");
        } catch (SQLException e) {
            System.out.println("SQL Exception : " + e.getMessage());
        }
    }

    public void intertexc(int i, exc vw) {
    }

}

excJSON.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

    final static String apiKey = "tUxsDwwjT72GgMLoJRprXlqc34wmOzX4";

    
    public exc getexc(boolean success, String timestamp, String base, String date, double GBP, double JPY, double EUR) {
    
       String urlStr = "https://api.apilayer.com/exchangerates_data/latest?symbols=GBP%2CJPY%2CEUR&base=USD"
                + "&apiKey=" + apiKey + "&success=" + success + "&timestamp=" + timestamp
                + "&base="+ base + "&date=" + date + "&GBP=" + GBP + "&JPY=" + JPY + "&EUR=" + EUR + "&_type=json";
        
       exc vl = new exc(); // 결과 데이터를 저장할 객체를 만듭니다.
        try {
            URL url = new URL(urlStr); 
            BufferedReader bf = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = "";
            String result="";
            
            while((line=bf.readLine())!=null){ 
                result=result.concat(line);
            }
           //System.out.println(result);
            
           
            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObj = (JSONObject) jsonParser.parse(result);
            JSONObject parse_response = (JSONObject) jsonObj.get("response");
            
            
            JSONObject obj;
            String category;
        
            vl.timestamp = timestamp;
            vl.base = base;
            vl.date = date;
            
            for(int i = 0; i < parse_response.size(); i++) {
                obj = (JSONObject) parse_response.get(i);
                category = (String)obj.get("category"); 
            
                
                switch(category) {
                    case "timestamp":
                        vl.timestamp = (obj.get("fcstValue")).toString();
                        break;
                    case "base":
                        vl.base = (obj.get("fcstValue")).toString();
                        break;
                    case "date":
                        vl.date = (obj.get("fcstValue")).toString();
                        break;
                }
            }

        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException : " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IOException : " + e.getMessage());
        } catch (ParseException e) {
            System.out.println("ParseException : " + e.getMessage());       
        }
        
        
        return vl;
    }
}

*Help me please. I think error in excJSON.java. I want make exchangerate program. I want fix error.

error code : IOException : Server returned HTTP response code: 403 for URL: https://api.apilayer.com/exchangerates_data/latest?symbols=GBP%2CJPY%2CEUR&base=USD&apiKey=tUxsDwwjT72GgMLoJRprXlqc34wmOzX4&success=true&timestamp=1659072666&base=USD&date=2022-07-29&GBP=0.82011&JPY=133.000499&EUR=0.979105&_type=json

I assess "https://api.apilayer.com/exchangerates_data/latest?symbols=GBP%2CJPY%2CEUR&base=USD&apiKey=tUxsDwwjT72GgMLoJRprXlqc34wmOzX4&success=true&timestamp=1659072666&base=USD&date=2022-07-29&GBP=0.82011&JPY=133.000499&EUR=0.979105&_type=json" but http say {"message":"No API key found in request"} but i surely receive apikey and api.*

Upvotes: 1

Views: 206

Answers (0)

Related Questions