-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlConnectionManager.java
More file actions
105 lines (94 loc) · 2.55 KB
/
UrlConnectionManager.java
File metadata and controls
105 lines (94 loc) · 2.55 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
package Maps;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import javax.swing.JOptionPane;
public class UrlConnectionManager {
public static HttpURLConnection getHttpUrlconnection(String serverURL,boolean post) {
HttpURLConnection httpurlc = null;
try {
URL url = new URL(serverURL);
URLConnection urlc = url.openConnection();
httpurlc = (HttpURLConnection) urlc;
httpurlc.setDoOutput(true);
if (!post)
{
httpurlc.setRequestMethod("GET");
}
else
{
httpurlc.setRequestMethod("POST");
}
httpurlc.setRequestProperty(
"Content-Type", "application/octet-stream");
httpurlc.connect();
} catch (MalformedURLException mfue) {
//logger.error("MalformedURLException :" + mfue);
return null;
} catch (SocketTimeoutException ste) {
//logger.error("SocketTimeoutException :" + ste);
return null;
} catch (IOException ioe) {
//logger.error("IOException :" + ioe);
return null;
}
return httpurlc;
}
public static void sendRequest(HttpURLConnection httpurlc, Object request) {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(httpurlc.getOutputStream());
oos.writeObject(request);
oos.flush();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Exception"+e.getMessage());
} finally {
try {
oos.close();
} catch (IOException ioe) {
//logger.error("IOException :" + ioe);
}
}
}
public static Object readResponse(HttpURLConnection httpurlc)
throws IOException {
ObjectInputStream ois = null;
Object respObj = null;
try {
ois = new ObjectInputStream(httpurlc.getInputStream());
respObj = ois.readObject();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Exception"+e.getMessage());
} finally {
ois.close();
}
return respObj;
}
/*public static InputStream readXmlResponse(HttpURLConnection httpurlc)
throws IOException {
InputStream in = null;
try {
in = httpurlc.getInputStream();
} catch (Exception e) {
if (null != in) {
in.close();
}
}
return in;
}*/
public static void closeConnection(HttpURLConnection httpurlc) {
if (null != httpurlc) {
httpurlc.disconnect();
}
}
}