Server.UrlEncode和HttpUtility.UrlEncode之间有区别吗?
如果我希望使用System.Net.HttpClient提交http get请求,似乎没有api添加参数,这是正确的吗?
是否有任何简单的api可用于构建查询字符串,该字符串不涉及构建名称值集合和url编码那些然后最终连接它们?我希望使用类似RestSharp的api(即AddParameter(..))
如何将对象序列化为查询字符串格式?我似乎无法在谷歌上找到答案.谢谢.
这是我将序列化的对象作为示例.
public class EditListItemActionModel
{
public int? Id { get; set; }
public int State { get; set; }
public string Prefix { get; set; }
public string Index { get; set; }
public int? ParentID { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找将查询字符串从HTTP GET请求转换为字典的最简单方法,然后再返回.
我认为一旦它以字典形式对查询进行各种操作就会更容易,但我似乎有很多代码只是为了进行转换.有推荐的方法吗?
在网址的某处有一个&sortBy = 6.如何在单击按钮时将其更新为&sortBy = 4或&sortBy = 2?我是否需要编写自定义字符串函数来创建正确的重定向网址?
如果我只需要附加一个查询字符串变量,我会这样做
string completeUrl = HttpContext.Current.Request.Url.AbsoluteUri + "&" + ...
Response.Redirect(completeUrl);
Run Code Online (Sandbox Code Playgroud)
但我想要做的是修改现有的querystring变量.
常规的.Net框架在System.Web程序集中包含HttpUtility.UrlEncode,在Silverlight中,它似乎被移动到System.Windows.Browser.但是在Windows Phone 7中(我认为它与Silverlight相同)我似乎无法找到适当的方式来UrlEncode.Windows Phone 7环境中既没有前面提到的程序集也可用.
使用以下代码我得到一个很好的格式化字符串:
Request.QueryString.ToString
Run Code Online (Sandbox Code Playgroud)
给我一些像:&hello = worldµsoft =糟透了
但是,当我使用此代码将集合克隆到另一个对象(相同类型)时,我从ToString()方法返回Type().
System.Collections.Specialized.NameValueCollection variables = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (!string.IsNullOrEmpty(variables["sid"]))
variables.Remove("sid");
Response.Write(variables.ToString());
Run Code Online (Sandbox Code Playgroud)
是否有更整洁的方式输出它而不是手动查看和构建字符串?
您好我尝试在C#(Post)中编写HTTP请求,但我需要有关错误的帮助
Expl:我想将DLC文件的内容发送到服务器并重新发送解密的内容.
C#代码
public static void decryptContainer(string dlc_content)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dcrypt.it/decrypt/paste");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
writer.Write("content=" + dlc_content);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我收到了html请求
<form action="/decrypt/paste" method="post">
<fieldset>
<p class="formrow">
<label for="content">DLC content</label>
<input id="content" name="content" type="text" value="" />
</p>
<p class="buttonrow"><button type="submit">Submit »</button></p>
</fieldset>
</form>
Run Code Online (Sandbox Code Playgroud)
错误信息:
{
"form_errors": {
"__all__": [
"Sorry, an error …Run Code Online (Sandbox Code Playgroud) .Net的System.Web.HttpUtility类定义了以下函数来将查询字符串解析为NameValueCollection:
public static NameValueCollection ParseQueryString(string query);
Run Code Online (Sandbox Code Playgroud)
是否有任何功能可以反向(即将a NameValueCollection转换为查询字符串)?
使用keyvaluepair构建的字符串是这样的:"name1 = v1&name2 = v2&name3 = v3"
我在做什么:
var sb = new StringBuilder();
foreach (var name in nameValues)
{
sb.AppendFormat("{0}={1}&", name.Key, name.Value);
}
//remove last '&' sign, this is what i think is ugly
sb.ToString().Remove(lastIndex);
Run Code Online (Sandbox Code Playgroud)
任何优雅的方法,以避免'&'符号的最后删除声明?
c# ×7
asp.net ×5
query-string ×4
.net ×2
asp.net-mvc ×1
collections ×1
http ×1
httprequest ×1
object ×1
post ×1
silverlight ×1
urlencode ×1