12 |
import java.net.URLConnection; |
import java.net.URLConnection; |
13 |
import java.net.URLDecoder; |
import java.net.URLDecoder; |
14 |
import java.net.URLEncoder; |
import java.net.URLEncoder; |
15 |
|
import java.nio.charset.Charset; |
16 |
import java.util.HashMap; |
import java.util.HashMap; |
17 |
import java.util.Map; |
import java.util.Map; |
18 |
|
|
70 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
71 |
|
|
72 |
boolean isFirst = true; |
boolean isFirst = true; |
73 |
for (String key : params.keySet()) { |
for (Map.Entry<String,String> entry : params.entrySet()) { |
74 |
if (isFirst) { |
if (isFirst) { |
75 |
isFirst = false; |
isFirst = false; |
76 |
} else { |
} else { |
77 |
sb.append("&"); |
sb.append("&"); |
78 |
} |
} |
79 |
|
|
80 |
String val = params.get(key); |
|
81 |
sb.append(key); |
sb.append( entry.getKey() ); |
82 |
sb.append("="); |
sb.append("="); |
83 |
sb.append( encode(val) ); |
sb.append( encode( entry.getValue() ) ); |
84 |
|
|
85 |
|
|
86 |
} |
} |
115 |
URLConnection connection = url.openConnection(); |
URLConnection connection = url.openConnection(); |
116 |
|
|
117 |
connection.setConnectTimeout(timeout); |
connection.setConnectTimeout(timeout); |
118 |
|
connection.setReadTimeout(timeout); |
119 |
InputStream is = connection.getInputStream(); |
InputStream is = connection.getInputStream(); |
120 |
|
|
121 |
return readInputStream(is); |
return readInputStream(is); |
129 |
connection.setConnectTimeout(timeout); |
connection.setConnectTimeout(timeout); |
130 |
|
|
131 |
OutputStream os = connection.getOutputStream(); |
OutputStream os = connection.getOutputStream(); |
132 |
os.write( data.getBytes() ); |
os.write( data.getBytes(Charset.forName("UTF-8")) ); |
133 |
|
|
134 |
InputStream is = connection.getInputStream(); |
InputStream is = connection.getInputStream(); |
135 |
|
|
139 |
|
|
140 |
static byte[] readInputStream(InputStream is) throws IOException{ |
static byte[] readInputStream(InputStream is) throws IOException{ |
141 |
byte buffer[] = new byte[1024]; |
byte buffer[] = new byte[1024]; |
142 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
//try /; //start buffer size - instead of default 32bytes |
143 |
try { |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) { |
144 |
int count; |
int count; |
145 |
|
|
146 |
while ( (count = is.read(buffer)) != -1) { |
while ( (count = is.read(buffer)) != -1) { |
147 |
baos.write(buffer, 0, count); |
baos.write(buffer, 0, count); |
148 |
} |
} |
149 |
} finally { |
is.close(); |
150 |
try { |
return baos.toByteArray(); |
151 |
is.close(); |
} |
|
baos.close(); |
|
|
} catch (Exception e) {} //never mind if close throws an exception |
|
|
} |
|
|
|
|
|
return baos.toByteArray(); |
|
152 |
} |
} |
153 |
|
|
154 |
} |
} |