如果我希望使用System.Net.HttpClient提交http get请求,似乎没有api添加参数,这是正确的吗?
是否有任何简单的api可用于构建查询字符串,该字符串不涉及构建名称值集合和url编码那些然后最终连接它们?我希望使用类似RestSharp的api(即AddParameter(..))
我有一个ASP.NET页面,它在查询字符串中包含许多参数:
search.aspx?q=123&source=WebSearch
Run Code Online (Sandbox Code Playgroud)
这将显示搜索结果的第一页.现在,在该页面的呈现中,我想显示一组链接,允许用户跳转到搜索结果中的不同页面.我可以简单地通过追加做这个&page=1或&page=2等
它变得复杂的地方是我想保留原始页面中输入查询字符串的每个参数,除了我正在尝试更改的那个.其他组件使用的url中可能还有其他参数,我尝试替换的值可能已经定义过,也可能没有定义:
search.aspx?q=123&source=WebSearch&page=1&Theme=Blue
Run Code Online (Sandbox Code Playgroud)
在这种情况下,要生成到下一页结果的链接,我想更改page=1为,page=2同时保持查询字符串的其余部分不变.
有没有内置的方法来做到这一点,还是我需要手动完成所有的字符串解析/重组?
我有一个NameValueCollection初始化的usercontrol,如下所示:
private NameValueCollection _nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
Run Code Online (Sandbox Code Playgroud)
当我调用ToString()它时,它会生成一个正确的查询字符串,我可以将其用于更新的URL.
但是,当我NameValueCollection像这样复制via它的构造函数时:
var nameValues = new NameValueCollection(_nameValues);
Run Code Online (Sandbox Code Playgroud)
然后尝试形成一个网址:
var newUrl = String.Concat(_rootPath + "?" + nameValues.ToString());
Run Code Online (Sandbox Code Playgroud)
它会输出一个这样的网址:
" http://www.domain.com?System.Collections.Specialized.NameValueCollection "
如何复制a NameValueCollection以使ToString()方法输出所需的结果?
我正在使用字典来保存一些参数,我发现不可能序列化任何实现IDictionary的东西(无法序列化IDictionary).
作为一种解决方法,我想将字典转换为字符串以进行序列化,然后在需要时转换回字典.
因为我正在努力改进我的LINQ,这似乎是一个很好的地方,但我不知道如何开始.
这是我在没有LINQ的情况下实现的方法:
/// <summary>
/// Get / Set the extended properties of the FTPS processor
/// </summary>
/// <remarks>Can't serialize the Dictionary object so convert to a string (http://msdn.microsoft.com/en-us/library/ms950721.aspx)</remarks>
public Dictionary<string, string> FtpsExtendedProperties
{
get
{
Dictionary<string, string> dict = new Dictionary<string, string>();
// Get the Key value pairs from the string
string[] kvpArray = m_FtpsExtendedProperties.Split('|');
foreach (string kvp in kvpArray)
{
// Seperate the key and value to build the dictionary
string[] pair = kvp.Split(','); …Run Code Online (Sandbox Code Playgroud) 我有以下 JSON,必须将其转换为 GET 请求的 URL 参数。
这里给出了一个例子,但是由于这个对象的复杂性,line_items_attributes每个对象可能有多个具有如图所示的给定值,我很难传递正确的对象。
我还尝试过序列化 JSON 对象并传递该值,但这也没有解决问题。
{
"purchase_invoice":
{
"date":"14/04/2015",
"due_date":"14/04/2015",
"contact_id":500,
"contact_name":"TestContact",
"reference":"TestReference",
"line_items_attributes":[
{
"unit_price":10.00,
"quantity":1,
"description":"TestLineItemAttDesc",
"tax_code_id":1,
"ledger_account_id":501,
"tax_rate_percentage":19.0,
"tax_amount":1.60
}]
}
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索了一段时间但没有太多运气。任何见解都值得赞赏和欢迎!
这是调用一个不支持 JSON 格式传入数据的 API,因此在服务器端执行此操作或更改 Web 服务以支持 JSON 格式数据是不可能的。