forked from deniskin82/httpsqs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpsqs_client.java
More file actions
117 lines (97 loc) · 3.09 KB
/
Httpsqs_client.java
File metadata and controls
117 lines (97 loc) · 3.09 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* 不包括keep-alive的部分,get方法只能获得数据,无法获得pos
* 作者:李博 [email protected]
*/
import java.io.*;
import java.net.*;
class Httpsqs_client {
private String server, port, charset;
public Httpsqs_client(String server, String port, String charset) {
this.server = server;
this.port = port;
this.charset = charset;
}
private String doprocess(String urlstr) {
URL url = null;
try {
url = new URL(urlstr);
} catch (MalformedURLException e) {
return "The httpsqs server must be error";
}
try {
BufferedReader instream = new BufferedReader(new InputStreamReader(url.openStream()));
String s = null;
StringBuffer result = new StringBuffer("");
while((s = instream.readLine()) != null)
{
result.append(s);
}
instream.close();
return result.toString();
} catch (IOException e) {
return "Get data error";
}
}
public String maxqueue(String queue_name, String num) {
String urlstr = "http://" + this.server + ":" + this.port + "/?name=" + queue_name + "&opt=maxqueue&num=" + num;
String result = null;
result = this.doprocess(urlstr);
return result;
}
public String reset(String queue_name) {
String urlstr = "http://" + this.server + ":" + this.port + "/?name=" + queue_name + "&opt=reset";
String result = null;
result = this.doprocess(urlstr);
return result;
}
public String view(String queue_name, String pos) {
String urlstr = "http://" + this.server + ":" + this.port + "/?charset=" + this.charset + "&name=" + queue_name + "&opt=view&pos=" + pos;
String result = null;
result = this.doprocess(urlstr);
return result;
}
public String status(String queue_name) {
String urlstr = "http://" + this.server + ":" + this.port + "/?name=" + queue_name + "&opt=status";
String result = null;
result = this.doprocess(urlstr);
return result;
}
public String get(String queue_name) {
String urlstr = "http://" + this.server + ":" + this.port + "/?charset=" + this.charset + "&name=" + queue_name + "&opt=get";
String result = null;
result = this.doprocess(urlstr);
return result;
}
public String put(String queue_name, String data) {
String urlstr = "http://" + this.server + ":" + this.port + "/?name=" + queue_name + "&opt=put";
URL url = null;
try {
url = new URL(urlstr);
} catch (MalformedURLException e) {
return "The httpsqs server must be error";
}
URLConnection conn = null;
try {
conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter out = null;
out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.flush();
out.close();
} catch (IOException e) {
return "Put data error";
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
return line;
}
} catch (IOException e) {
return "Get return data error";
}
return null;
}
}