使用java在网页上输入特定数据

时间:2016-04-23 21:01:40

标签: javascript java json javascript-events put

  

我有以下代码,使用get方法从网页中提取作业截止日期,任何人都可以告诉我如何在网页上更改日期和PUT。我正在使用JAVA从网页上使用HttpURLConnection获取数据。code is avaible here

public class project {
public static HttpURLConnection connection;
public static void main(String[] args) throws Exception {
try {
String auth = fileReader("auth.dat");
// put the required courseID
String courseId="10300000000000133";
String url1= "https://canvas.instructure.com/api/v1/courses/";
URL url = new URL(url1+courseId+"/assignments/10300000000043867/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer "+auth);
connection.setRequestMethod("GET");
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response code:" + connection.getResponseCode());

System.out.println("Response message:"+ connection.getResponseMessage());

File file = new File("JSON1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file); 
// Writes the content to the file

    // Read the response:
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));
    String line;
    StringBuffer response = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        response.append(line);
    }
    reader.close();
    System.out.println(response.toString());
    writer.write(response.toString()); 
    writer.flush();
    writer.close();
}
catch (MalformedURLException e1) {
    e1.printStackTrace();
} catch (IOException e2) {
    e2.printStackTrace();
}
jsonParser();
}

public static String fileReader(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        line = br.readLine();

    return sb.toString();
} finally {
    br.close();
}
}

public static void jsonParser() throws Exception{
try{
    String jsonData = fileReader("JSON1.txt");
    JSONParser parser = new JSONParser();
    //System.out.println( "\n    id"+"\t\t\t"+ "dueAt" +"\t"+ "assignmentName"+"\n");
    // take each value from the json array separately
    JSONObject innerObj = (JSONObject)parser.parse(jsonData);

    // get a number from the JSON object
    long id = (long) innerObj.get("id");         
    // get a String from the JSON object
    String assignmentName = (String) innerObj.get("name");
    String dueAt = (String) innerObj.get("due_at");
    System.out.println( "Assignment Due Date:"+"\t"+ dueAt +"\t"+ assignmentName);
    }catch(ParseException ex){
        ex.printStackTrace();           
    }catch (IOException e2) {
        e2.printStackTrace();
    }
}
}

0 个答案:

没有答案