Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
PiHttpClient.java
Go to the documentation of this file.
1 /* Copyright 2013 Eric Ptak - trouch.com
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13 */
14 
15 package com.trouch.webiopi.client;
16 
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.net.HttpURLConnection;
21 import java.net.URL;
22 
23 public class PiHttpClient extends PiClient {
24  public final static int DEFAULT_PORT = 8000;
25 
26  public PiHttpClient(String host) {
27  super("http", host, DEFAULT_PORT);
28  }
29 
30  public PiHttpClient(String host, int port) {
31  super("http", host, port);
32  }
33 
34  @Override
35  public String sendRequest(String method, String path) throws Exception {
36  BufferedReader reader = null;
37  try {
38  URL url = new URL(this.urlBase + path);
39  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
40  connection.setRequestMethod(method);
41  if (this.auth != null) {
42  connection.setRequestProperty("Authorization", this.auth);
43  }
44 
45  // read the output from the server
46  reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
47  StringBuilder stringBuilder = new StringBuilder();
48 
49  String line = null;
50  while ((line = reader.readLine()) != null) {
51  stringBuilder.append(line).append('\n');
52  }
53  return stringBuilder.toString();
54  } catch (Exception e) {
55  e.printStackTrace();
56  throw e;
57  } finally {
58  if (reader != null) {
59  try {
60  reader.close();
61  } catch (IOException ioe) {
62  ioe.printStackTrace();
63  }
64  }
65  }
66  }
67 
68 }
string url
Definition: setup.py:21
String sendRequest(String method, String path)