简体   繁体   中英

How to replace “http:” with “https:” in a string with C#?

I've stored all URLs in my application with "http://" - I now need to go through and replace all of them with "https:". Right now I have:

    foreach (var link in links)
        {
            if (link.Contains("http:"))
            {
                /// do something, slice or replace or what?
            }
        }

I'm just not sure what the best way to update the string would be. How can this be done?

If you're dealing with uris, you probably want to use UriBuilder since doing a string replace on structured data like URIs is not a good idea.

var builder = new UriBuilder(link);
builder.Scheme = "https";
Uri modified = builder.Uri;

It's not clear what the type of links is, but you can create a new collection with the modified uris using linq:

IEnumerable<string> updated = links.Select(link => {
    var builder = new UriBuilder(link);
    builder.Scheme = "https";
    return builder.ToString();
});

The problem is your strings are in a collection, and since strings are immutable you can't change them directly. Since you didn't specify the type of links ( List ? Array ?) the right answer will change slightly. The easiest way is to create a new list:

links = links.Select(link => link.Replace("http://","https://")).ToList();

However if you want to minimize the number of changes and can access the string by index you can just loop through the collection:

for(int i = 0; i < links.Length; i++ )
{
    links[i] = links[i].Replace("http://","https://");
}

based on your current code, link will not be replace to anything you want because it is read only (see here: Why can't I modify the loop variable in a foreach? ). instead use for

for(int a = 0; a < links.Length; a++ )
{
    links[a] = links[a].Replace("http:/","https:/")
}

使用string.Replace和一些LINQ:

var httpsLinks = links.Select(l=>l.Replace("http://", "https://");

http://myserver.xom/login.aspx?returnurl= http %3a%2f%2fmyserver.xom%2fmyaccount.aspx&q1=a%20b%20c&q2=c%2b%2b

What about the urls having also url in the querystring part? I think we should also replace them. And because of the url encoding-escaping this is the hard part of the job.

private void BlaBla()
{
    // call the replacing function
    Uri myNewUrl = ConvertHttpToHttps(myOriginalUrl);
}

private Uri ConvertHttpToHttps(Uri originalUri)
{
    Uri result = null;
    int httpsPort = 443;// if needed assign your own value or implement it as parametric 

    string resultQuery = string.Empty;
    NameValueCollection urlParameters = HttpUtility.ParseQueryString(originalUri.Query);

    if (urlParameters != null && urlParameters.Count > 0)
    {
        StringBuilder sb = new StringBuilder();
        foreach (string key in urlParameters)
        {
            if (sb.Length > 0)
                sb.Append("&");

            string value = urlParameters[key].Replace("http://", "https://");
            string valuEscaped = Uri.EscapeDataString(value);// this is important
            sb.Append(string.Concat(key, "=", valuEscaped));
        }
        resultQuery = sb.ToString();
    }

    UriBuilder resultBuilder = new UriBuilder("https", originalUri.Host, httpsPort, originalUri.AbsolutePath);
    resultBuilder.Query = resultQuery;

    result = resultBuilder.Uri;
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM