追加值查询字符串 [英] Append values to query string
问题描述
我已经在列表中设置URL的类似于以下的人的
I have set of URL's similar to the ones below in a list
- 的http://somesite.com/backup/lol.php?id=1&server=4&location=us
- http://somesite.com/news.php?article= 1安培;郎= EN
- http://somesite.com/backup/lol.php?id=1&server=4&location=us
- http://somesite.com/news.php?article=1&lang=en
我设法使用下面的代码来获取查询字符串:
I have managed to get the query strings using the following code:
myurl = longurl.Split('?');
NameValueCollection qs = HttpUtility.ParseQueryString(myurl [1]);
foreach (string lol in qs)
{
// results will return
}
但它只返回像
ID 服务器 位置的参数等的基础上提供的网址。
But it only returns the parameters like id, server, location and so on based on the URL provided.
我需要的是添加/追加值,以现有的查询字符串。
What I need is to add / append values to the existing query strings.
例如用网址:
的 http://somesite.com/backup/index.php?action=login&attempts=1
我需要改变的值查询字符串参数:
I need to alter the values of the query string parameters:
行动= login1
尝试= 11
正如你所看到的,我已经追加1的每个值。我需要从与他们不同的查询字符串的字符串得到一组URL的和值添加到结尾&放各项参数;再次将它们添加到列表中。
As you can see, I have appended "1" for each value. I need to get a set of URL's from a string with different query strings in them and add a value to each parameter at the end & again add them to a list.
推荐答案
您可以使用的 HttpUtility.ParseQueryString
方法和的 UriBuilder
它提供了一个很好的方式与查询字符串参数工作,而不必担心这样的事情解析,网址编码,...:
You could use the HttpUtility.ParseQueryString
method and an UriBuilder
which provides a nice way to work with query string parameters without worrying about things like parsing, url encoding, ...:
string longurl = "http://somesite.com/news.php?article=1&lang=en";
var uriBuilder = new UriBuilder(longurl);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["action"] = "login1";
query["attempts"] = "11";
uriBuilder.Query = query.ToString();
longurl = uriBuilder.ToString();
// "http://somesite.com:80/news.php?article=1&lang=en&action=login1&attempts=11"
这篇关于追加值查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!