📄 paramcall.java
字号:
package com.bv.api;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.Properties;import sun.misc.BASE64Encoder;/** * An APICall subtype that pases data to the server as parameters in the URL. * @author C. Oattes */public abstract class ParamCall extends APICall { protected ParamCall() { super(); } protected String connect(String uaid, String serverURL) throws ParamNotPresentException, InvalidParameterException, MalformedURLException, IOException { HttpURLConnection connection; BufferedReader responseReader; URL connectionURL = null; checkParams(uaid); connectionURL = constructURL(uaid, serverURL); connection = (HttpURLConnection) connectionURL.openConnection(); String username = ""; String password = ""; Properties values = new Properties(); try { FileInputStream is = new FileInputStream("values.properties"); values.load(is); username = values.getProperty("username"); password = values.getProperty("password"); String encoding = new BASE64Encoder().encode((username + ":" + password).getBytes()); connection.setRequestProperty ("Authorization", "Basic " + encoding); } catch(IOException ioe) { //Ignore if values.properties not present } connection.setConnectTimeout(5000); connection.connect(); responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); //Read the response from the server - should be only 1 line String serverResponse = ""; while(responseReader.ready()) { serverResponse += responseReader.readLine() + "\n" ; } connection.disconnect(); System.out.println("Server response: " + serverResponse); return serverResponse; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -