android-java中的网络请求:get与post
2014-08-04 16:59
411 查看
对于经常用的网络请求你是否觉得很有解决方式但是实现起来有点困难!
这里对于一般的请求实现其代码,也许你有更好的处理方式,分享!!!
public static String sendGet(String url,String params){
String result="";
BufferedReader in=null;
try{
String urlName=url+"?"+params;
URL realUrl=new URL(urlName);
URLConnection conn=realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;SV1)");
conn.setRequestProperty("connection", "Keep-Alive");
conn.connect();
Map<String,List<String>> map=conn.getHeaderFields();
for(String key:map.keySet()){
System.out.println(key+"-->"+map.get(key));
}
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLine())!=null){
result+="\n"+line;
}
}catch(Exception e){
System.out.println("发送GET请求出现异常!"+e);
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String sendPost(String url,String params){
PrintWriter out=null;
String result="";
BufferedReader in=null;
try{
URL realUrl=new URL(url);
URLConnection conn=realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;SV1)");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setDoInput(true);
conn.setDoOutput(true);
out=new PrintWriter(conn.getOutputStream());
out.print(params);
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLine())!=null){
result+="\n"+line;
}
}catch(Exception e){
System.out.println("发送POST请求出现异常!"+e);
e.printStackTrace();
}finally{
try {
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
这里对于一般的请求实现其代码,也许你有更好的处理方式,分享!!!
public static String sendGet(String url,String params){
String result="";
BufferedReader in=null;
try{
String urlName=url+"?"+params;
URL realUrl=new URL(urlName);
URLConnection conn=realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;SV1)");
conn.setRequestProperty("connection", "Keep-Alive");
conn.connect();
Map<String,List<String>> map=conn.getHeaderFields();
for(String key:map.keySet()){
System.out.println(key+"-->"+map.get(key));
}
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLine())!=null){
result+="\n"+line;
}
}catch(Exception e){
System.out.println("发送GET请求出现异常!"+e);
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String sendPost(String url,String params){
PrintWriter out=null;
String result="";
BufferedReader in=null;
try{
URL realUrl=new URL(url);
URLConnection conn=realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;SV1)");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setDoInput(true);
conn.setDoOutput(true);
out=new PrintWriter(conn.getOutputStream());
out.print(params);
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLine())!=null){
result+="\n"+line;
}
}catch(Exception e){
System.out.println("发送POST请求出现异常!"+e);
e.printStackTrace();
}finally{
try {
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
相关文章推荐
- Android-封装post和get 网络请求
- Android开发网络请求之GET和POST
- Android okHttp网络请求之Get/Post请求
- Android网络请求库okHttp进行post、get请求
- Android----------OkHttp网络请求框架Get和Post简单封装(泛型)
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- android网络应用URLConnection提交请求GET和POST
- Android okHttp网络请求之Get/Post请求(一)
- JAVA基础知识之网络编程——-网络基础(Java的http get和post请求,多线程下载)
- java网络编程中向指定URL发送GET POST请求示例
- Android 网络请求通用的get与post方式
- Android中的http带参数网络请求,GET和POST
- Android网络框架-Volley(四) 使用get和post方法发送json请求
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- Android使用Httpclient网络请求,get,post方法
- java网络请求GET和POST方式
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- Java网络请求(get/post)工具类实现的两种方式
- Android okHttp 实战(一):okHttp网络请求之Get/Post请求
- Android联网app必用Get请求和Post请求的区别,网络编程代码实现