-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnection.java
More file actions
93 lines (78 loc) · 3.2 KB
/
Connection.java
File metadata and controls
93 lines (78 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
public class Connection {
private final String USER_AGENT = "SigSoftBot/1.0"; // Identify ourselves to websites
private URL url;
private HttpURLConnection connection;
public String responseBody = "";
public int responseStatus;
public Connection (String url) {
try {
this.url = new URL(url);
this.connection = (HttpURLConnection) this.url.openConnection(); // We want to specifically deal with an HTTP connection
connection.setRequestProperty("User-Agent", USER_AGENT); // Set our User-Agent header (google it if you want to learn more)
} catch (MalformedURLException urlEx) {
System.err.println("The entered URL was invalid.");
System.err.println(urlEx.toString());
} catch (IOException ioEx) {
System.err.println("Error initializing connection to server.");
System.err.println(ioEx.toString());
}
}
public void get() {
try {
connection.setRequestMethod("GET");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseLine;
do {
responseLine = br.readLine();
if (responseLine != null) {
responseBody += responseLine+"\n";
}
} while (responseLine != null);
br.close();
//responseBody = response.toString();
responseStatus = connection.getResponseCode();
} catch (IOException ioEx) {
System.err.println("IO error communicating with server.");
}
}
public void post(long hits, long total)
{
try{
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
BufferedOutputStream wr = new BufferedOutputStream (connection.getOutputStream());
String bodyString = "hits=" + hits + "&total=" + total;
wr.write(bodyString.getBytes(Charset.forName("UTF-8")));
wr.flush ();
wr.close ();
}
catch (IOException ioEx) {
System.err.println("IO error communicating with server.");
System.err.println(ioEx.toString());
}
try {
responseStatus = connection.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseLine;
do {
responseLine = br.readLine();
if (responseLine != null) {
responseBody += responseLine+"\n";
}
} while (responseLine != null);
br.close();
//responseBody = response.toString();
} catch (IOException ioEx) {
System.err.println("IO error communicating with server.");
System.err.println(ioEx.toString());
}
System.out.println(responseBody);
}
}