//post表单请求
public static String post(String url, Map<String, String> form){
HttpURLConnection conn = null;
PrintWriter pw = null ;
BufferedReader rd = null ;
StringBuilder out = new StringBuilder();
StringBuilder sb = new StringBuilder();
String line = null ;
String response = null;
for (String key : form.keySet()) {
if(out.length()!=0){
out.append("&");
}
out.append(key).append("=").append(form.get(key));
}
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(30000);
conn.setUseCaches(false);
conn.connect();
pw = new PrintWriter(conn.getOutputStream());
pw.print(out.toString());
pw.flush();
if (conn.getResponseCode() == 200) {
rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(pw != null){
pw.close();
}
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
//get请求
public static String get(String url){
HttpURLConnection conn = null;
BufferedReader rd = null ;
StringBuilder sb = new StringBuilder ();
String line = null ;
String response = "0";
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(30000);
conn.setUseCaches(false);
conn.connect();
if (conn.getResponseCode() == 200) {
rd = new BufferedReader( new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
//get请求,返回ByteArr
public static byte[] getStream(String url){
HttpURLConnection conn = null;
byte[] fileByte = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(30000);
conn.setUseCaches(false);
conn.connect();
if (conn.getResponseCode() == 200) {
fileByte = input2byte(conn.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(conn != null){
conn.disconnect();
}
}
return fileByte;
}
public static final byte[] input2byte(InputStream inStream)
throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
}
UrlConnection使用
最新推荐文章于 2023-05-25 21:21:31 发布