System.Net.WebUtility.UrlEncode でURLエンコードすると空白文字(スペース)が"+"の文字でエンコードされてしまう - C#
System.Net.WebUtility.UrlEncode でURLエンコードするとスペースが"+"の文字でエンコードされてしまう現象について紹介します。
概要
System.Net.WebUtility.UrlEncode メソッドを利用して文字列をURLエンコードするとスペースが "+" の文字でエンコードされます。
通常は問題ありませんが、利用シーンによってはスペースを"%20"でエンコードしたい場合があります。
この記事では利用するメソッドによってURLエンコードの結果がどのように変わるか確認します。
現象の確認 : System.Net.WebUtility.UrlEncode() でURLエンコードした場合
UI
下図のフォームを作成します。テキストボックスを2つ、ボタンを1つ配置します。
コード
下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace URLEncodeDemoDotNet5
{
public partial class FormWebUtilityUrlEncode : Form
{
public FormWebUtilityUrlEncode()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string input = textBox1.Text;
string result = System.Net.WebUtility.UrlEncode(input);
textBox2.Text = result;
}
}
}
解説
System.Net.WebUtility.UrlEncode() メソッドを利用して文字列をURLエンコードします。結果をテキストボックスに表示します。
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。
上部のテキストにスペースのあるURLを入力します。今回は下記を入力します。
http://www.ipentec.com/test dir/index.html
[button1]をクリックします。下のテキストボックスに結果が表示されます。
http%3A%2F%2Fwww.ipentec.com%2Ftest+dir%2Findex.html
空白(スペース)の文字が "+" でURLエンコードされていることが確認できます。
対処方法
空白を "+" ではなく "%20" でURLエンコードしたい場合には、
System.Uri.EscapeDataString()
メソッドを利用します。
プログラム例 : System.Uri.EscapeDataString() でエンコードする
UI
下図のフォームを作成します。
コード
下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace URLEncodeDemoDotNet5
{
public partial class FormEscapeDataString : Form
{
public FormEscapeDataString()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string input = textBox1.Text;
string result = System.Uri.EscapeDataString(input);
textBox2.Text = result;
}
}
}
解説
System.Uri.EscapeDataString() メソッドを利用して文字列をURLエンコードします。結果をテキストボックスに表示します。
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。
上部のテキストにスペースのあるURLを入力します。今回は下記を入力します。
http://www.ipentec.com/test dir/index.html
[button1]をクリックします。下のテキストボックスに結果が表示されます。
http%3A%2F%2Fwww.ipentec.com%2Ftest%20dir%2Findex.html
空白(スペース)の文字が "%20" でURLエンコードされていることが確認できます。
参考 : System.Web.HttpUtility.UrlEncode() でURLエンコードした場合
System.Web.HttpUtility.UrlEncode() でURLエンコードした場合の結果も確認します。
UI
先のプログラムと同じUIを利用します。
コード
下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace URLEncodeDemoDotNet5
{
public partial class FormHttpUtilityUrlEncode : Form
{
public FormHttpUtilityUrlEncode()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string input = textBox1.Text;
string result = System.Web.HttpUtility.UrlEncode(input);
textBox2.Text = result;
}
}
}
解説
System.Web.HttpUtility.UrlEncode() メソッドを利用して文字列をURLエンコードします。結果をテキストボックスに表示します。
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。
上部のテキストにスペースのあるURLを入力します。今回は下記を入力します。
http://www.ipentec.com/test dir/index.html
[button1]をクリックします。下のテキストボックスに結果が表示されます。
http%3a%2f%2fwww.ipentec.com%2ftest+dir%2findex.html
空白(スペース)の文字が "+" でURLエンコードされていることが確認できます。
System.Net.WebUtility.UrlEncode() メソッドでは、エンコードの文字は大文字でエンコードされますが、
System.Web.HttpUtility.UrlEncode() メソッドでは、エンコードされた文字が小文字になる違いがあります。
結果の比較
http://www.ipentec.com/test dir/index.html
http%3A%2F%2Fwww.ipentec.com%2Ftest+dir%2Findex.html
http%3A%2F%2Fwww.ipentec.com%2Ftest%20dir%2Findex.html
http%3a%2f%2fwww.ipentec.com%2ftest+dir%2findex.html
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用