你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发之短信窃听器

[日期:2011-12-27] 来源:Linux社区  作者:chenglong0513 [字体:大 中 小]
 
  1. package cn.class3g.utils;  
  2.   
  3.    
  4.   
  5. import java.io.BufferedReader;  
  6.   
  7. import java.io.ByteArrayOutputStream;  
  8.   
  9. import java.io.DataOutputStream;  
  10.   
  11. import java.io.InputStream;  
  12.   
  13. import java.io.InputStreamReader;  
  14.   
  15. import java.io.OutputStream;  
  16.   
  17. import java.net.HttpURLConnection;  
  18.   
  19. import java.net.InetAddress;  
  20.   
  21. import java.net.Socket;  
  22.   
  23. import java.net.URL;  
  24.   
  25. import java.net.URLEncoder;  
  26.   
  27. import java.util.ArrayList;  
  28.   
  29. import java.util.List;  
  30.   
  31. import java.util.Map;  
  32.   
  33.    
  34.   
  35. import org.apache.http.HttpResponse;  
  36.   
  37. import org.apache.http.NameValuePair;  
  38.   
  39. import org.apache.http.client.HttpClient;  
  40.   
  41. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  42.   
  43. import org.apache.http.client.methods.HttpPost;  
  44.   
  45. import org.apache.http.impl.client.DefaultHttpClient;  
  46.   
  47. import org.apache.http.message.BasicNameValuePair;  
  48.   
  49.    
  50.   
  51. public class SocketHttpRequester {  
  52.   
  53.    /**  
  54.   
  55.     * 发送xml数据  
  56.   
  57.     * @param path 请求地址  
  58.   
  59.     * @param xml xml数据  
  60.   
  61.     * @param encoding 编码  
  62.   
  63.     * @return  
  64.   
  65.     * @throws Exception  
  66.   
  67.     */  
  68.   
  69.    public static byte[] postXml(String path, String xml, String encoding) throws Exception{  
  70.   
  71.       byte[] data = xml.getBytes(encoding);  
  72.   
  73.       URL url = new URL(path);  
  74.   
  75.       HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  76.   
  77.       conn.setRequestMethod("POST");  
  78.   
  79.       conn.setDoOutput(true);  
  80.   
  81.       conn.setRequestProperty("Content-Type", "text/xml; charset="+ encoding);  
  82.   
  83.       conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
  84.   
  85.       conn.setConnectTimeout(5 * 1000);  
  86.   
  87.       OutputStream outStream = conn.getOutputStream();  
  88.   
  89.       outStream.write(data);  
  90.   
  91.       outStream.flush();  
  92.   
  93.       outStream.close();  
  94.   
  95.       if(conn.getResponseCode()==200){  
  96.   
  97.         return readStream(conn.getInputStream());  
  98.   
  99.       }  
  100.   
  101.       return null;  
  102.   
  103.    }  
  104.   
  105.      
  106.   
  107.    /**  
  108.   
  109.     * 直接通过HTTP协议提交数据到服务器,实现如下面表单提交功能:  
  110.   
  111.     *   <FORM METHOD=POST ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data">  
  112.   
  113.         <INPUT TYPE="text" NAME="name">  
  114.   
  115.         <INPUT TYPE="text" NAME="id">  
  116.   
  117.         <input type="file" name="imagefile"/>  
  118.   
  119.           <input type="file" name="zip"/>  
  120.   
  121.        </FORM>  
  122.   
  123.     * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)  
  124.   
  125.     * @param params 请求参数 key为参数名,value为参数值  
  126.   
  127.     * @param file 上传文件  
  128.   
  129.     */  
  130.   
  131.    public static boolean post(String path, Map<String, String> params, FormFile[] files) throws Exception{       
  132.   
  133.         final String BOUNDARY = "---------------------------7da2137580612"; //数据分隔线  
  134.   
  135.         final String endline = "--" + BOUNDARY + "--\r\n";//数据结束标志  
  136.   
  137.           
  138.   
  139.         int fileDataLength = 0;  
  140.   
  141.         for(FormFile uploadFile : files){//得到文件类型数据的总长度  
  142.   
  143.         StringBuilder fileExplain = new StringBuilder();  
  144.   
  145.            fileExplain.append("--");  
  146.   
  147.            fileExplain.append(BOUNDARY);  
  148.   
  149.            fileExplain.append("\r\n");  
  150.   
  151.            fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");  
  152.   
  153.            fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");  
  154.   
  155.            fileExplain.append("\r\n");  
  156.   
  157.            fileDataLength += fileExplain.length();  
  158.   
  159.         if(uploadFile.getInStream()!=null){  
  160.   
  161.             fileDataLength += uploadFile.getFile().length();  
  162.   
  163.           }else{  
  164.   
  165.             fileDataLength += uploadFile.getData().length;  
  166.   
  167.           }  
  168.   
  169.         }  
  170.   
  171.         StringBuilder textEntity = new StringBuilder();  
  172.   
  173.         for (Map.Entry<String, String> entry : params.entrySet()) {//构造文本类型参数的实体数据  
  174.   
  175.             textEntity.append("--");  
  176.   
  177.             textEntity.append(BOUNDARY);  
  178.   
  179.             textEntity.append("\r\n");  
  180.   
  181.             textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");  
  182.   
  183.             textEntity.append(entry.getValue());  
  184.   
  185.             textEntity.append("\r\n");  
  186.   
  187.         }  
  188.   
  189.         //计算传输给服务器的实体数据总长度  
  190.   
  191.         int dataLength = textEntity.toString().getBytes().length + fileDataLength +  endline.getBytes().length;  
  192.   
  193.           
  194.   
  195.         URL url = new URL(path);  
  196.   
  197.         int port = url.getPort()==-1 ? 80 : url.getPort();  
  198.   
  199.         Socket socket = new Socket(InetAddress.getByName(url.getHost()), port);            
  200.   
  201.         OutputStream outStream = socket.getOutputStream();  
  202.   
  203.         //下面完成HTTP请求头的发送  
  204.   
  205.         String requestmethod = "POST "+ url.getPath()+" HTTP/1.1\r\n";  
  206.   
  207.         outStream.write(requestmethod.getBytes());  
  208.   
  209.         String accept = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";  
  210.   
  211.         outStream.write(accept.getBytes());  
  212.   
  213.         String language = "Accept-Language: zh-CN\r\n";  
  214.   
  215.         outStream.write(language.getBytes());  
  216.   
  217.         String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "\r\n";  
  218.   
  219.         outStream.write(contenttype.getBytes());  
  220.   
  221.         String contentlength = "Content-Length: "+ dataLength + "\r\n";  
  222.   
  223.         outStream.write(contentlength.getBytes());  
  224.   
  225.         String alive = "Connection: Keep-Alive\r\n";  
  226.   
  227.         outStream.write(alive.getBytes());  
  228.   
  229.         String host = "Host: "+ url.getHost() +":"+ port +"\r\n";  
  230.   
  231.         outStream.write(host.getBytes());  
  232.   
  233.         //写完HTTP请求头后根据HTTP协议再写一个回车换行  
  234.   
  235.         outStream.write("\r\n".getBytes());  
  236.   
  237.         //把所有文本类型的实体数据发送出来  
  238.   
  239.         outStream.write(textEntity.toString().getBytes());           
  240.   
  241.         //把所有文件类型的实体数据发送出来  
  242.   
  243.         for(FormFile uploadFile : files){  
  244.   
  245.         StringBuilder fileEntity = new StringBuilder();  
  246.   
  247.            fileEntity.append("--");  
  248.   
  249.            fileEntity.append(BOUNDARY);  
  250.   
  251.            fileEntity.append("\r\n");  
  252.   
  253.            fileEntity.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");  
  254.   
  255.            fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");  
  256.   
  257.            outStream.write(fileEntity.toString().getBytes());  
  258.   
  259.            if(uploadFile.getInStream()!=null){  
  260.   
  261.            byte[] buffer = new byte[1024];  
  262.   
  263.            int len = 0;  
  264.   
  265.            while((len = uploadFile.getInStream().read(buffer, 0, 1024))!=-1){  
  266.   
  267.                outStream.write(buffer, 0, len);  
  268.   
  269.            }  
  270.   
  271.            uploadFile.getInStream().close();  
  272.   
  273.            }else{  
  274.   
  275.            outStream.write(uploadFile.getData(), 0, uploadFile.getData().length);  
  276.   
  277.            }  
  278.   
  279.            outStream.write("\r\n".getBytes());  
  280.   
  281.         }  
  282.   
  283.         //下面发送数据结束标志,表示数据已经结束  
  284.   
  285.         outStream.write(endline.getBytes());  
  286.   
  287.           
  288.   
  289.         BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  290.   
  291.         if(reader.readLine().indexOf("200")==-1){//读取web服务器返回的数据,判断请求码是否为200,如果不是200,代表请求失败  
  292.   
  293.         return false;  
  294.   
  295.         }  
  296.   
  297.         outStream.flush();  
  298.   
  299.         outStream.close();  
  300.   
  301.         reader.close();  
  302.   
  303.         socket.close();  
  304.   
  305.         return true;  
  306.   
  307.    }  
  308.   
  309.      
  310.   
  311.    /**  
  312.   
  313.     * 提交数据到服务器  
  314.   
  315.     * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)  
  316.   
  317.     * @param params 请求参数 key为参数名,value为参数值  
  318.   
  319.     * @param file 上传文件  
  320.   
  321.     */  
  322.   
  323.    public static boolean post(String path, Map<String, String> params, FormFile file) throws Exception{  
  324.   
  325.       return post(path, params, new FormFile[]{file});  
  326.   
  327.    }  
  328.   
  329.    /**  
  330.   
  331.     * 提交数据到服务器  
  332.   
  333.     * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)  
  334.   
  335.     * @param params 请求参数 key为参数名,value为参数值  
  336.   
  337.     * @param encode 编码  
  338.   
  339.     */  
  340.   
  341.    public static byte[] postFromHttpClient(String path, Map<String, String> params, String encode) throws Exception{  
  342.   
  343.       List<NameValuePair> formparams = new ArrayList<NameValuePair>();//用于存放请求参数  
  344.   
  345.       for(Map.Entry<String, String> entry : params.entrySet()){  
  346.   
  347.         formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
  348.   
  349.       }  
  350.   
  351.       UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, encode);  
  352.   
  353.       HttpPost httppost = new HttpPost(path);  
  354.   
  355.       httppost.setEntity(entity);  
  356.   
  357.       HttpClient httpclient = new DefaultHttpClient();//看作是浏览器  
  358.   
  359.       HttpResponse response = httpclient.execute(httppost);//发送post请求      
  360.   
  361.       return readStream(response.getEntity().getContent());  
  362.   
  363.    }  
  364.   
  365.    
  366.   
  367.    /**  
  368.   
  369.     * 发送请求  
  370.   
  371.     * @param path 请求路径  
  372.   
  373.     * @param params 请求参数 key为参数名称 value为参数值  
  374.   
  375.     * @param encode 请求参数的编码  
  376.   
  377.     */  
  378.   
  379.    public static byte[] post(String path, Map<String, String> params, String encode) throws Exception{  
  380.   
  381.       //String params = "method=save&name="+ URLEncoder.encode("老毕", "UTF-8")+ "&age=28&";//需要发送的参数  
  382.   
  383.       StringBuilder parambuilder = new StringBuilder("");  
  384.   
  385.       if(params!=null && !params.isEmpty()){  
  386.   
  387.         for(Map.Entry<String, String> entry : params.entrySet()){  
  388.   
  389.            parambuilder.append(entry.getKey()).append("=")  
  390.   
  391.               .append(URLEncoder.encode(entry.getValue(), encode)).append("&");  
  392.   
  393.         }  
  394.   
  395.         parambuilder.deleteCharAt(parambuilder.length()-1);  
  396.   
  397.       }  
  398.   
  399.       byte[] data = parambuilder.toString().getBytes();  
  400.   
  401.       URL url = new URL(path);  
  402.   
  403.       HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  404.   
  405.       conn.setDoOutput(true);//允许对外发送请求参数  
  406.   
  407.       conn.setUseCaches(false);//不进行缓存  
  408.   
  409.       conn.setConnectTimeout(5 * 1000);  
  410.   
  411.       conn.setRequestMethod("POST");  
  412.   
  413.       //下面设置http请求头  
  414.   
  415.       conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");  
  416.   
  417.       conn.setRequestProperty("Accept-Language", "zh-CN");  
  418.   
  419.       conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");  
  420.   
  421.       conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
  422.   
  423.       conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
  424.   
  425.       conn.setRequestProperty("Connection", "Keep-Alive");  
  426.   
  427.         
  428.   
  429.       //发送参数  
  430.   
  431.       DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());  
  432.   
  433.       outStream.write(data);//把参数发送出去  
  434.   
  435.       outStream.flush();  
  436.   
  437.       outStream.close();  
  438.   
  439.       if(conn.getResponseCode()==200){  
  440.   
  441.         return readStream(conn.getInputStream());  
  442.   
  443.       }  
  444.   
  445.       return null;  
  446.   
  447.    }  
  448.   
  449.      
  450.   
  451.    /**  
  452.   
  453.     * 读取流  
  454.   
  455.     * @param inStream  
  456.   
  457.     * @return 字节数组  
  458.   
  459.     * @throws Exception  
  460.   
  461.     */  
  462.   
  463.    public static byte[] readStream(InputStream inStream) throws Exception{  
  464.   
  465.       ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  466.   
  467.       byte[] buffer = new byte[1024];  
  468.   
  469.       int len = -1;  
  470.   
  471.       while( (len=inStream.read(buffer)) != -1){  
  472.   
  473.         outSteam.write(buffer, 0, len);  
  474.   
  475.       }  
  476.   
  477.       outSteam.close();  
  478.   
  479.       inStream.close();  
  480.   
  481.       return outSteam.toByteArray();  
  482.   
  483.    }  
  484.   
  485. }  


 

 
  1. package cn.class3g.utils;  
  2.   
  3.    
  4.   
  5. import java.io.ByteArrayOutputStream;  
  6.   
  7. import java.io.InputStream;  
  8.   
  9.    
  10.   
  11. public class StreamTool {  
  12.   
  13.    
  14.   
  15.    /**  
  16.   
  17.     * 从输入流读取数据  
  18.   
  19.     * @param inStream  
  20.   
  21.     * @return  
  22.   
  23.     * @throws Exception  
  24.   
  25.     */  
  26.   
  27.    public static byte[] readInputStream(InputStream inStream) throws Exception{  
  28.   
  29.       ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  30.   
  31.       byte[] buffer = new byte[1024];  
  32.   
  33.       int len = 0;  
  34.   
  35.       while( (len = inStream.read(buffer)) !=-1 ){  
  36.   
  37.         outSteam.write(buffer, 0, len);  
  38.   
  39.       }  
  40.   
  41.       outSteam.close();  
  42.   
  43.       inStream.close();  
  44.   
  45.       return outSteam.toByteArray();  
  46.   
  47.    }  
  48.   
  49. }  
linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款