如何替换url参数c#

时间:2021-05-01 10:28:43
https://www.stote.it/bais/%ID%

I have a URL like the above and a method that takes key. I want to replace ID with key and make a get request. I can make the get request if I manually replace ID with a value, but I want to do it in code. Any help?

我有一个像上面这样的URL和一个带密钥的方法。我想用密钥替换ID并发出get请求。如果我用一个值手动替换ID,我可以发出get请求,但我想在代码中执行此操作。有帮助吗?

private String getEmailTemplate(string key)
{
    string html = string.Empty;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        html = reader.ReadToEnd();
    }

    return html;

1 个解决方案

#1


3  

If url is arbitrary, make it https://www.stote.it/bais/{0} and use String.Format(url, key).

如果url是任意的,请将其设为https://www.stote.it/bais/{0}并使用String.Format(url,key)。

This way if you ever add more arguments, there is no complicated string replacement.

这样,如果您添加更多参数,则没有复杂的字符串替换。

#1


3  

If url is arbitrary, make it https://www.stote.it/bais/{0} and use String.Format(url, key).

如果url是任意的,请将其设为https://www.stote.it/bais/{0}并使用String.Format(url,key)。

This way if you ever add more arguments, there is no complicated string replacement.

这样,如果您添加更多参数,则没有复杂的字符串替换。