-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequests.java
More file actions
77 lines (69 loc) · 2.69 KB
/
Requests.java
File metadata and controls
77 lines (69 loc) · 2.69 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
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
public class Requests {
private static String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
/*private static void writeStream(OutputStream out) throws IOException{
String output = "Hello world";
//out.write(output.getBytes());
out.flush();
}*/
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://roger123.pythonanywhere.com/dummy/");
HttpURLConnection urlConnection = (HttpURLConnection) yahoo.openConnection();
try {
urlConnection.setDoOutput(true);
//urlConnection.setChunkedStreamingMode(0);
//OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
//writeStream(out);
urlConnection.addRequestProperty("Content-Type", "application/"+"POST");
String query="42";
if(query!=null) {
urlConnection.setRequestProperty("Content-Length", Integer.toString(query.length()));
urlConnection.getOutputStream().write(query.getBytes());
}
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
System.out.println(readStream(in));
} finally {
urlConnection.disconnect();
}
/*HttpURLConnection con = (HttpURLConnection) yahoo.openConnection();
con.setRequestMethod("GET");
Map<String, String> parameters = new HashMap<>();
parameters.put("param1", "val");
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();*/
/*URLConnection yc = yahoo.openConnection();
//yc.setRequestProperty("hello", "moto");
yc.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
yc.getOutputStream());
out.write("string=" + "hello");
out.close();
/*PrintWriter writer = new PrintWriter(yc.getOutputStream());
writer.println("hello");
writer.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();*/
}}