我需要在类库程序集中编码URL,我不想引用System.Web.URL包含多个空格
https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quote where symbol in ("YHOO","AAPL")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
Run Code Online (Sandbox Code Playgroud)
当我使用System.Net.WebUtility.UrlEncode()时,空格将替换为"+",这不起作用.我需要用%20替换它们
如何在不引用System.Web的情况下实现此目的?
我正在使用NSURLComponents,我似乎无法获得正确编码的查询值.我需要最终的URL来表示+as %2B.
let baseUrl = NSURL(string: "http://www.example.com")
let components = NSURLComponents(URL: baseUrl, resolvingAgainstBaseURL: true)
components.queryItems = [ NSURLQueryItem(name: "name", value: "abc+def") ]
XCTAssertEqual(components!.string!, "http://www.example.com?connectionToken=abc%2Bdef")
Run Code Online (Sandbox Code Playgroud)
失败!
输出等于:
http://www.example.com?connectionToken=abc+def
Run Code Online (Sandbox Code Playgroud)
不
http://www.example.com?connectionToken=abc%2Bdef
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种变化,我似乎无法将其输出%2B.
我有这个用于登录请求的函数。
private login(params: LoginParams): Promise<any> {
const loginHeaders: HttpHeaders = new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
.set('site', 'first');
const loginCredentials = new HttpParams()
.set('j_username', params.username)
.set('j_password', params.password);
const requestUrl = this.appConfig.baseUrl + 'restoftheurl';
return this.http
.post(requestUrl, loginCredentials.toString(),
{headers: loginHeaders, responseType: 'text'})
.toPromise();
}
Run Code Online (Sandbox Code Playgroud)
如果密码中有一个加号 (+),它会被编码为一个空格符号,然后请求将失败,因为这是一个错误的凭证。如何保留加号?我究竟做错了什么?
如果我有这个参数添加到URL
params = { name: 'John Key' }
并使用方法to_param:
params.to_param
=> "name=John+Key"
Run Code Online (Sandbox Code Playgroud)
关键是所使用的服务无法正确读取'+',而是需要'%20' name=John%20Key:何时将空间编码为加号(+)或%20?
有没有办法在没有使用的情况下使用'%20'返回参数gsub?
在ASP.NET Web API的控制器中给出以下方法:
[HttpGet]
[ApiRoute("resource/{id}/end-point)]
public IHttpActionResult MethodName (int id, string clientTimeZone)
{
...
}
Run Code Online (Sandbox Code Playgroud)
每当我向http:// localhost:5684/api/v1/resource/1/end-point?client_timezone =%2B0500提交GET请求时,clientTimezone将作为%2B0500传递给clientTimeZone,并将编码后的"+"符号解析为空间角色.为什么ASP.NET不能从URI解码+?
在标题中,我有"ContentType = application/json"和一个承载令牌
我试图让"+0500"进入我的方法,但它变成了"0500"
我使用URLCodec的Apache的百科全书编解码器编码的网址,但其编码空间,+还不如%20
为什么?什么是解决方案?
我试图从我使用Ruby on Rails的哈希创建http参数,我尝试过使用URI.encode_www_form(params),但这并没有正确生成参数.
下面是我的哈希
params['Name'.to_sym] = 'Nia Kun'
params['AddressLine1'.to_sym] = 'Address One'
params['City'.to_sym] = 'City Name'
Run Code Online (Sandbox Code Playgroud)
This method converts space to +,我想要的是它 convert space with %20
我收到了,"Name=Nia+Kun&AddressLine1=Address+One&City=City+Name"但我需要将这些空格转换为%20
我有一个Spring应用程序接收类似的请求http://localhost/foo?email=foo+bar@example.com.这会触发一个大致如下所示的控制器:
@RestController
@RequestMapping("/foo")
public class FooController extends Controller {
@GetMapping
public void foo(@RequestParam("email") String email) {
System.out.println(email)
}
}
Run Code Online (Sandbox Code Playgroud)
当我可以访问时email,它已被转换为foo bar@example.com而不是原始的foo+bar@example.com.根据何时将空间编码为加号(+)或%20?这应该只发生在内容所在的请求中application/x-www-form-urlencoded.我的请求的内容类型为application/json.请求的完整MIME标头如下所示:
=== MimeHeaders ===
accept = application/json
content-type = application/json
user-agent = Dashman Configurator/0.0.0-dev
content-length = 0
host = localhost:8080
connection = keep-alive
Run Code Online (Sandbox Code Playgroud)
为什么Spring会将plus解码为空格?如果这是它应该工作的方式,为什么它不像编写%2B请求时那样编码?
我发现了这个关于它的错误报告:https://jira.spring.io/browse/SPR-6291这可能暗示这是在3.0.5版本上修复的,我使用的是Spring> 5.0.0.我可能会误解错误报告的某些内容.
我还发现了关于这些值的RestTemplate处理的讨论:https://jira.spring.io/browse/SPR-5516(我的客户端正在使用RestTemplate).
所以,我的问题是,为什么Spring这样做?我该如何禁用它?我应该禁用它还是应该在客户端编码加号,即使请求是json?
只是为了澄清,我在这里任何地方都不使用HTML或JavaScript.有一个Spring Rest Controller,客户端是Spring的RestTemplate,UriTemplate或者UriComponentsBuilder …
我最近创建了一个关于如何在 URL 中使用像/和这样的符号的+问题,但这让我想到了另一个问题,我如何替换 URL 中的空格,为什么?
如果我的网址是something.com/Find/this is my search,那为什么是错误的?为什么我们需要将其更改为something.com/Find/this+is+my+search
我已经搜索和尝试解决方案超过 5 个小时了。我看的每个地方的答案都是一样的,使用httputility.urlencodeor Uri.escapeDataString。但我试过这样做:
string encode = Uri.EscapeDataString(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode );
string encode = HttpUtility.UrlEncode(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode );
string encode = encode.replace(" ", "+")
Response.Redirect("/Find/" + encode);
Run Code Online (Sandbox Code Playgroud)
这些都不起作用,它们不会用任何东西替换空格(string.replace 会这样做,但这也会导致字符串更改,这意味着它无法在下一页的数据库中找到值)。
如果我对整个 URL 进行编码,那么我/将全部转为 %,这显然不是我想要的。
我需要的
If I redirect like this Response.Redirect("/Find/" + search);.
And I make a search like this "Social media".
I then get the queryString on …Run Code Online (Sandbox Code Playgroud) 这肯定已经被问过很多次了,但提供的解决方案似乎不起作用。
输入:
sample text
Run Code Online (Sandbox Code Playgroud)
输出:
sample%20text
Run Code Online (Sandbox Code Playgroud)
我使用的是建议的HttpUtility.UrlEncode(),但它返回的不是所需的输出:
sample+text
Run Code Online (Sandbox Code Playgroud)
我做错了什么,还是这个方法不再有效?