Home > Backend Development > Golang > How to solve the problem of gbk garbled code in golang http request

How to solve the problem of gbk garbled code in golang http request

PHPz
Release: 2023-04-24 10:34:30
Original
1530 people have browsed it

When using Go language to make http requests, if you encounter gbk encoding problems, garbled characters may appear. In this case, we need to understand what causes the garbled code and explore solutions.

  1. The principle of GBK encoding

GBK is a character encoding method, which is one of the encoding methods based on Chinese characters. GBK encoding adopts a multi-byte encoding method. Each Chinese character occupies two bytes, of which the first byte and the second byte are called high byte and low byte respectively.

  1. The http package of Go language handles GBK encoding issues

In the http package of Go language, if we directly use http.Get or http.Post to make a request, it will By default, utf-8 encoding is used. When we encounter gbk encoding, we need to use gbk.Decode for conversion.

The following is a sample code that uses the http package for gbk encoding:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "strings"
    "golang.org/x/text/encoding/simplifiedchinese"
)

// get请求函数
func HttpGet(url string) (string, error) {
    resp, err := http.Get(url)
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return "", err
    }
    return string(body), nil
}

// post请求函数
func HttpPost(url, param string) (string, error) {
    payload := strings.NewReader(param)
    req, err := http.NewRequest("POST", url, payload)
    if err != nil {
        return "", err
    }
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return "", err
    }
    return string(body), nil
}

func main() {
    url := "http://www.example.com"
    body, err := HttpGet(url)
    if err != nil {
        fmt.Println(err)
    }
    gbkBody, _ := simplifiedchinese.GBK.NewDecoder().String(body)
    fmt.Println(gbkBody)
}
Copy after login

In the sample code, we use the gbk decoder function in the simplifiedchinese package and perform the following on the body returned by the request decoding operation. For post requests, it can be handled in the same way.

  1. Solving the GBK garbled problem in the browser

In addition to processing in the Go language, we can also specify the encoding method in the browser to solve the problem Garbled code problem in gbk encoding.

For example: In the Chrome browser, you can make the following settings:

(1) Open the menu bar: Customize and control Google Chrome.

(2) Select Settings, select Advanced at the bottom, and then select Language under Languages.

(3) Click Add Languages ​​and select Chinese (Simplified, China).

(4) Select Options under the language and select Chinese fonts (such as Song Dynasty or Microsoft Yahei).

(5) Open the website in the page. If the garbled code has been resolved, the setting is successful.

  1. Summary

When using Go language to make http requests, gbk encoding is a common problem. By understanding the gbk encoding principle and the method of decoding in the Go language, we can effectively solve the garbled problem of gbk encoding. Settings in the browser can also provide us with a solution that can be chosen on a case-by-case basis.

The above is the detailed content of How to solve the problem of gbk garbled code in golang http request. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template