NEWS

Lấy thông tin chi tiết website (file get content hay CURL) trong Visual Basic . NET - Tìm kiếm thông tin doanh nghiệp qua mã số thuế

Lấy thông tin chi tiết website (file get content hay CURL) trong Visual Basic . NET - Tìm kiếm thông tin doanh nghiệp qua mã số thuế
Đăng bởi: Thảo Meo - Lượt xem: 22972 10:19:43, 25/10/2016VB.NET   In bài viết

Chào các bạn. Hôm nay, mình xin hướng dẫn các bạn cách lấy thông tin từ website từ VB.NET  (CURL hay file get content trong PHP), bạn nào code bằng Csharp có thể convert code từ VB.NET sang Csharp nha . Thông tin, chúng ta có thể lấy thông tin từ website khác về phần mềm qua RSS. Các bạn có thể xem bài viết trước

Lấy tỷ giá trực tuyến Vietcombank 

Bây giờ mình xin hướng dẫn các bạn cách lấy thông tin của công ty qua mã số thuế, lấy dữ liệu từ trang web http://thongtincongty.com.

Ví dụ khi nhập mã số thuế trên trang web thongtincongty.com ta sẽ có được thông tin như hình bên dưới:

thong-tin-cong-ty-ma-so-thue

Bây giờ, mình sẽ viết ứng dụng có giao diện như hình bên dưới:

+ Gồm một 1 text box để nhập mã số thuế cần tìm kiếm vào

+ Một button để sử lý sự kiện

+ Và 1 webbrowser để load thông tin trả về từ câu truy vấn

Các bạn, thiết kế giao diện ứng dụng như hình bên dưới:

Curl - file get content in vb.net or csharp

Các bạn có thể xem video demo của ứng dụng:

Ở trên website, khi ta nhập mã số thuế vào và nhấn nút tìm kiếm, thì form trên sẽ submit theo dạng POST, và trả về dữ liệu thông tin chi tiết của công ty

thong-tin-cong-ty

Bây giờ, mình chỉ hiển thị phần thông tin trong class Jumbotron là ok.

Dưới đây là code của chương trình:

 
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
    Dim loginCookie As CookieContainer
    Private Sub btn_timkiem_Click(sender As Object, e As EventArgs) Handles btn_timkiem.Click
        Dim postData As String = "q=" & txtquery.Text
        Dim tempCookie As New CookieContainer
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte() = encoding.GetBytes(postData)
        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://www.thongtincongty.com"), HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.CookieContainer = tempCookie
        postReq.ContentType = "application/x-www-form-urlencoded"
        postReq.Referer = "http://www.thongtincongty.com"
        postReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
        postReq.ContentLength = byteData.Length

        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close()
        Dim postresponse As HttpWebResponse
        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)

        tempCookie.Add(postresponse.Cookies)
        loginCookie = tempCookie
        Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
        Dim thepage As String = postreqreader.ReadToEnd
        RichTextBox1.Text = thepage
        WebBrowser1.DocumentText = RichTextBox1.Text
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim divs = WebBrowser1.Document.Body.GetElementsByTagName("div")
        Dim i As Integer = 0
        Dim d As HtmlElement
        For Each d In divs
            If d.GetAttribute("className") = "jumbotron" Then
                WebBrowser2.DocumentText = d.InnerHtml
                i = i + 1
            End If
        Next
        If i = 0 Then
            WebBrowser2.DocumentText = "
MÃ SỐ THUẾ KHÔNG TỒN TẠI
" End If End Sub End Class

Chúc các bạn thành công. Mọi câu hỏi thắc mắc đến bài viết xin truy cập http://hoidap.laptrinhvb.net để được support.yes

ĐỪNG QUÊN LIKE AND SHARE NHA CÁC BẠN. 

CÁM ƠN CÁC BẠN ĐÃ THEO DÕI.

LINK DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

Lấy thông tin chi tiết website (file get content hay CURL) trong Visual Basic . NET - Tìm kiếm thông tin doanh nghiệp qua mã số thuế
Đăng bởi: Thảo Meo - Lượt xem: 22972 10:19:43, 25/10/2016VB.NET   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp