java socket HTTPClient GET

來源:互聯網
上載者:User

 

public class HttpClientMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Socket socket;
        try {
            InetAddress hostAdd=InetAddress.getByName("192.168.6.5");
            socket=new Socket(hostAdd,80);
            OutputStream out=socket.getOutputStream();
            InputStream in=socket.getInputStream();

            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            //baos.write("data=abc\r\n".getBytes());
            //System.out.println("Data Lenth:"+baos.toByteArray().length);
            
            byte[] bytes = ("GET / HTTP/1.1\n" +
                    "Host:192.168.6.5\n" +
                    "Connection:close\n" +
                    "User-agent:Mozilla/4.0\n" +
                    "Accept-language:zh-cn\n" +
                    "\n").getBytes("UTF-8");

            out.write(bytes);
            out.flush();
            byte[] bt=new byte[1024];
            InputStreamReader isr;
            int len;
            baos.reset();
            while((len=in.read(bt))>-1){
                baos.write(bt, 0, len);
            }
            Vector<String> v=GetStartEndValue(baos.toString(),"\\(","\\)", 0);
            if (v.size()>0){
                System.out.println(v.firstElement());
            }
            baos.close();
            in.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

     /// <summary>
    /// 獲得字串中開始和結束字串中間得值
    /// </summary>
    /// <param name="str">字串</param>
    /// <param name="s">開始</param>
    /// <param name="e">結束</param>
    ///<param name="azimuth">匹配內容是否包含開始與結束符號,0:不包含  1:包含</param>
    /// <returns></returns> 
    
    public static Vector<String> GetStartEndValue(String str, String s, String e,int azimuth){
        Pattern pattern;
        if (azimuth==1){
            pattern = Pattern.compile("(?=(" + s + "))[.\\s\\S]*?(?<=(" + e + "))");
        }else{
            pattern = Pattern.compile("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))");
        }
        Matcher matcher = pattern.matcher(str);
        StringBuffer buffer = new StringBuffer();
        Vector<String> vector=new Vector<String>();
        while(matcher.find()){             
            vector.add(matcher.group());
        }
        return vector;
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.