I try to send my post request by rust’s rewquest,but I found that some errors would occur when the Chinese strings in the form were received and responded to by the network.
The original string is “测试”,but in the web it is “娴嬭瘯”.
I think this is caused by the different encoding of my string and the website,the string encoding is UTF-8,but the web,is GBK.
This is my original code:
use futures::executor::block_on;
use reqwest::blocking::Client;
use scraper::{Html, Selector};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
block_on(search())?;
Ok(())
}
async fn search() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let test_str = "测试";
let form = [("searchkey", test_str)];
let res = client
.post("http://127.0.0.1:4567/modules/article/search.php")
.form(&form)
.send()?
.text()?;
println!("{res}");
Ok(())
}
And this is a part of HTML:
<div class="">
<h3 class="mytitle">SearchKey“<b class="hottext">娴嬭瘯</b>”,total:<b class="hottext"> 0 </b>recrods.</h3>
<div class="newbox">
<ul>
</ul>
</div>
</div>
I search in the Google,and find encoding
,I have a try,like this:
use encoding::{all::GBK, Encoding};
use futures::executor::block_on;
use reqwest::blocking::Client;
use scraper::{Html, Selector};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
block_on(search())?;
Ok(())
}
async fn search() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let test_str = "测试";
let test_str = test_str.as_bytes();
let test_str = GBK.decode(test_str, encoding::DecoderTrap::Strict)?;
let form = [("searchkey", test_str)];
let res = client
.post("http://127.0.0.1:4567/modules/article/search.php")
.form(&form)
.send()?
.text()?;
println!("{res}");
Ok(())
}
But response still like after.
So what shoud I do?
eastnosoul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.