直接上代码,调用的时候要放在线程中。
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(); } }}