博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android中利用HttpURLConnection进行Get、Post和Session读取页面。
阅读量:5787 次
发布时间:2019-06-18

本文共 2609 字,大约阅读时间需要 8 分钟。

直接上代码,调用的时候要放在线程中。

package slj.getsms;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.io.IOException;public class sljWork {	public static String GetDataFromKC(String loginUrl,String loginParam,String indexUrl)	{		byte[] btParam=loginParam.getBytes();		try{			URL url=new URL(loginUrl);			HttpURLConnection conn=(HttpURLConnection)url.openConnection();			conn.setConnectTimeout(3000);			conn.setRequestMethod("POST");			conn.setInstanceFollowRedirects(false);			OutputStream outStream=conn.getOutputStream();			outStream.write(btParam);			conn.connect();			String[] backHeader=conn.getHeaderField("Location").split(";");			String sessionId="user_login_id=dddd; "+backHeader[1].toUpperCase();			URL nextUrl=new URL(indexUrl);			HttpURLConnection nextConn=(HttpURLConnection)nextUrl.openConnection();			nextConn.addRequestProperty("Cookie",sessionId);			nextConn.connect();			BufferedReader bufReader=new BufferedReader(new InputStreamReader(nextConn.getInputStream(),"GBK"));			String backData="";			String line="";			while((line=bufReader.readLine())!=null)				backData+=line+"\r\n";			return backData;		}catch (IOException e){			return "ErrorInfo:"+e.getMessage().toString();		}	}	public static String GetDataByPost(String strUrl)	{		byte[] btPostData=new String("pname=slj&ppass=slj").getBytes();		try{			URL url=new URL(strUrl);			HttpURLConnection conn=(HttpURLConnection)url.openConnection();			conn.setConnectTimeout(3000);			conn.setRequestMethod("POST");			conn.setInstanceFollowRedirects(false);			OutputStream outStream=conn.getOutputStream();			outStream.write(btPostData);			conn.connect();			BufferedReader bufReader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));			String backData="";			String line="";			while((line=bufReader.readLine())!=null)				backData+=line+"\r\n";			return backData;		}catch(IOException e){			return "ErrorInfo:"+e.getMessage().toString();		}	}	public static String GetDataByGet(String strUrl)	{		try{			URL url=new URL(strUrl);			HttpURLConnection conn=(HttpURLConnection)url.openConnection();			conn.setConnectTimeout(3000);			conn.setRequestMethod("GET");			conn.connect();			BufferedReader bufReader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));			String backData="";			String line="";			while((line=bufReader.readLine())!=null)				backData+=line+"\r\n";			return backData;		}catch(IOException e){			return "ErrorInfo:"+e.getMessage().toString();		}	}}

 

转载于:https://www.cnblogs.com/ddddfpxx/p/6329407.html

你可能感兴趣的文章
XP 安装ORACLE
查看>>
八、 vSphere 6.7 U1(八):分布式交换机配置(vMotion迁移网段)
查看>>
我的友情链接
查看>>
JS中比较数字大小
查看>>
jQuery插件的开发
查看>>
基础,基础,还是基础之JAVA基础
查看>>
JS prototype 属性
查看>>
HTTP库Axios
查看>>
gen already exists but is not a source folder. Convert to a source folder or rename it 的解决办法...
查看>>
20个Linux服务器性能调优技巧
查看>>
填坑记:Uncaught RangeError: Maximum call stack size exceeded
查看>>
SpringCloud之消息总线(Spring Cloud Bus)(八)
查看>>
实时编辑
查看>>
KVO原理分析及使用进阶
查看>>
【348天】每日项目总结系列086(2018.01.19)
查看>>
【294天】我爱刷题系列053(2017.11.26)
查看>>
可替换元素和非可替换元素
查看>>
2016/08/25 The Secret Assumption of Agile
查看>>
(Portal 开发读书笔记)Portlet间交互-PortletSession
查看>>
搭建vsftpd服务器,使用匿名账户登入
查看>>