如何在httpwebrequest中编码url [英] How to encode url in httpwebrequest
本文介绍了如何在httpwebrequest中编码url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我如何使用c#在httprequest中对url进行编码,我们必须在url中使用paas参数。
如:http:// _ domain / api / sms /?uid = _uid&pwd = _pwd 。
如果我要将参数(_domain,_uid,_pwd)传递给这个字符串。那怎么办呢?
我也想知道这是必要的编码吗?
please tell me how to encode url in httprequest using c#, we we have to paas parameters in url.
like: http://_domain/api/sms/?uid=_uid&pwd=_pwd.
if i hav to pass parameters(_domain,_uid,_pwd) to this string.then how it can be done?
also i want to know that is it necessary to encode it?
推荐答案
UrlEncode
函数用于转义特殊字符,如''&''和''=''因此解析器不会将它们视为语法。它需要一个字符串(媒体类型text / plain)并返回一个字符串(媒体类型application / x-www-form-urlencoded)。
Encoding.UTF8.GetBytes用于将字符串(媒体类型application / x-www-form-urlencoded在我们的例子中)转换为字节数组,这是WebRequest API所期望的。
TheUrlEncode
function is used to escape especial characters like ''&'' and ''='' so a parser doesn''t consider them as syntax. It takes a string (media type text/plain) and returns a string (media type application/x-www-form-urlencoded).
Encoding.UTF8.GetBytes is used to convert the string (media type application/x-www-form-urlencoded in our case) into an array of bytes, which is what the WebRequest API expects.
用于Url Enoding /解码您需要使用WebUtily
类。
要查看其详细信息,请访问: MSDN WebUtility类 [ ^ ]
这里检查UrlDecode
和UrlEncode
。
但是根据你的要求,为了获得你使用的参数应该使用Request.Querystring
。 MSDN Request.Querystring [ ^ ]
示例用法;
For Url Enoding/Decoding you need to useWebUtily
class.
To see its details visit: MSDN WebUtility Class[^]
Here checkUrlDecode
andUrlEncode
.
But from what you ask in order to get the parameters that use you should useRequest.Querystring
. MSDN Request.Querystring[^]
An example usage;
if (Request.QueryString["id"] != null) {
try
{
id = int.Parse(Request.QueryString["id"]);
}
catch
{
// deal with it
}
}
好祝你好运,
OI
Good luck,
OI
这篇关于如何在httpwebrequest中编码url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文