zoukankan      html  css  js  c++  java
  • android中利用HttpURLConnection进行Get、Post和Session读取页面。

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

    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+"
    ";
    			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+"
    ";
    			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+"
    ";
    			return backData;
    		}catch(IOException e){
    			return "ErrorInfo:"+e.getMessage().toString();
    		}
    	}
    }
    
  • 相关阅读:
    使用jmeter进行接口测试
    jenkins默认插件
    【机器学习】关于PCA 昕
    雲瑞文件直链分享软件
    FOB离岸价
    【数据分享】某地区1959~2019年60年降雨量时间序列数据
    【数据分享】银行客户流失Bank Customer Churn数据
    【数据分享】糖尿病患者研究数据集
    【数据分享】维基百科Wiki负面有害评论(网络暴力)文本数据多标签分类挖掘可视化
    C# Speech
  • 原文地址:https://www.cnblogs.com/ddddfpxx/p/6329407.html
Copyright © 2011-2022 走看看