Làm cách nào để tách URL trong C#?

url-phân tích cú pháp-c

Trình phân tích URL C đơn giản được tạo cho mục đích nghiên cứu

Show

Đang chạy

$ make check
./url_parser http://google.com/
Protocol: 'http' - Host: 'google.com' - Port: '0' - Path: '/' - Query String: '(null)' Valid Host: 1 - IP: '173.194.118.66'

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2022, Daniel Stenberg, , et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * SPDX-License-Identifier: curl
 *
 ***************************************************************************/
/* 
 * Basic URL API use.
 * 
 */
#include 
#include 
 
#if !CURL_AT_LEAST_VERSION(7, 62, 0)
#error "this example requires curl 7.62.0 or later"
#endif
 
int main(void)
{
  CURLU *h;
  CURLUcode uc;
  char *host;
  char *path;
 
  h = curl_url(); /* get a handle to work with */
  if(!h)
    return 1;
 
  /* parse a full URL */
  uc = curl_url_set(h, CURLUPART_URL, "http://example.com/path/index.html", 0);
  if(uc)
    goto fail;
 
  /* extract host name from the parsed URL */
  uc = curl_url_get(h, CURLUPART_HOST, &host, 0);
  if(!uc) {
    printf("Host name: %s\n", host);
    curl_free(host);
  }
 
  /* extract the path from the parsed URL */
  uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
  if(!uc) {
    printf("Path: %s\n", path);
    curl_free(path);
  }
 
  /* redirect with a relative URL */
  uc = curl_url_set(h, CURLUPART_URL, "../another/second.html", 0);
  if(uc)
    goto fail;
 
  /* extract the new, updated path */
  uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
  if(!uc) {
    printf("Path: %s\n", path);
    curl_free(path);
  }
 
  fail:
  curl_url_cleanup(h); /* free url handle */
  return 0;
}

Chuỗi truy vấn là một tập hợp các ký tự được gắn vào cuối url, thường là sau dấu chấm hỏi. Các ký tự trong chuỗi truy vấn được sử dụng để mô tả loại thông tin mà người dùng đang tìm kiếm

Các chuỗi truy vấn thường được tạo thành từ một loạt các cặp khóa-giá trị, trong đó khóa được phân tách khỏi giá trị bằng dấu bằng (=)

Ví dụ: nếu bạn đang tìm kiếm "Chuỗi truy vấn trong URL là gì?" . https. //www. Google. com/search?q=What+is+a+query+string+in+a+URL%3F. "q" trong trường hợp này là tham số chuỗi truy vấn biểu thị truy vấn tìm kiếm của người dùng và giá trị của tham số đó là "Chuỗi truy vấn trong URL là gì?"

Chuỗi truy vấn thường được sử dụng để lưu trữ thông tin được sử dụng để tạo trang, chẳng hạn như ID người dùng hoặc ID phiên

Chỉ thị SPLIT-URL chia URL thành giao thức, quyền hạn, máy chủ, cổng, đường dẫn, tên tệp và truy vấn

cú pháp

là cột chứa URL

Ghi chú sử dụng

Chỉ thị SPLIT-URL sẽ phân tích cú pháp URL thành các thành phần của nó. Khi tách URL, lệnh sẽ tạo bảy cột mới bằng cách nối thêm vào tên cột ban đầu

  • column_protocol
  • column_authority
  • column_host
  • ________số 8
  • column_path
  • /***************************************************************************
     *                                  _   _ ____  _
     *  Project                     ___| | | |  _ \| |
     *                             / __| | | | |_) | |
     *                            | (__| |_| |  _ <| |___
     *                             \___|\___/|_| \_\_____|
     *
     * Copyright (C) 1998 - 2022, Daniel Stenberg, , et al.
     *
     * This software is licensed as described in the file COPYING, which
     * you should have received as part of this distribution. The terms
     * are also available at https://curl.se/docs/copyright.html.
     *
     * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     * copies of the Software, and permit persons to whom the Software is
     * furnished to do so, under the terms of the COPYING file.
     *
     * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     * KIND, either express or implied.
     *
     * SPDX-License-Identifier: curl
     *
     ***************************************************************************/
    /* 
     * Basic URL API use.
     * 
     */
    #include 
    #include 
     
    #if !CURL_AT_LEAST_VERSION(7, 62, 0)
    #error "this example requires curl 7.62.0 or later"
    #endif
     
    int main(void)
    {
      CURLU *h;
      CURLUcode uc;
      char *host;
      char *path;
     
      h = curl_url(); /* get a handle to work with */
      if(!h)
        return 1;
     
      /* parse a full URL */
      uc = curl_url_set(h, CURLUPART_URL, "http://example.com/path/index.html", 0);
      if(uc)
        goto fail;
     
      /* extract host name from the parsed URL */
      uc = curl_url_get(h, CURLUPART_HOST, &host, 0);
      if(!uc) {
        printf("Host name: %s\n", host);
        curl_free(host);
      }
     
      /* extract the path from the parsed URL */
      uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
      if(!uc) {
        printf("Path: %s\n", path);
        curl_free(path);
      }
     
      /* redirect with a relative URL */
      uc = curl_url_set(h, CURLUPART_URL, "../another/second.html", 0);
      if(uc)
        goto fail;
     
      /* extract the new, updated path */
      uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
      if(!uc) {
        printf("Path: %s\n", path);
        curl_free(path);
      }
     
      fail:
      curl_url_cleanup(h); /* free url handle */
      return 0;
    }
    
    0
  • {
      "url": "http://example.com:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING",
      "url_protocol": "http",
      "url_authority": "example.com:80",
      "url_host": "example.com",
      "url_port": 80,
      "url_path": "/docs/books/tutorial/index.html",
      "url_filename": "/docs/books/tutorial/index.html?name=networking",
      "url_query": "name=networking"
    }
    
    0

Nếu URL không thể được phân tích cú pháp chính xác, một ngoại lệ sẽ được ném. Nếu cột URL không tồn tại, các cột có giá trị

{
  "url": "http://example.com:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING",
  "url_protocol": "http",
  "url_authority": "example.com:80",
  "url_host": "example.com",
  "url_port": 80,
  "url_path": "/docs/books/tutorial/index.html",
  "url_filename": "/docs/books/tutorial/index.html?name=networking",
  "url_query": "name=networking"
}
1 sẽ được thêm vào bản ghi