質問
通常であれば、私のような用途:
HttpContext.Current.Server.UrlEncode("url");
しかしこれはコンソール-アプリケーション, HttpContext.Current
は必ず null
.
しおりを付けることができる方法も同じことが行われているか。
解決
他のヒント
したらいいと思います。.純んが、できない使用
HttpUtility.UrlEncode Method (String)
るかについてはこちらをご覧下さい:
このコードからイアン-ホプキンスはフレキシビリティを私にとってなくて追加の参照。ます。こちらはポートがC#ない方のための利用VB.NET:
/// <summary>
/// URL encoding class. Note: use at your own risk.
/// Written by: Ian Hopkins (http://www.lucidhelix.com)
/// Date: 2008-Dec-23
/// (Ported to C# by t3rse (http://www.t3rse.com))
/// </summary>
public class UrlHelper
{
public static string Encode(string str) {
var charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*'()"));
return Regex.Replace(str,
String.Format("[^{0}]", charClass),
new MatchEvaluator(EncodeEvaluator));
}
public static string EncodeEvaluator(Match match)
{
return (match.Value == " ")?"+" : String.Format("%{0:X2}", Convert.ToInt32(match.Value[0]));
}
public static string DecodeEvaluator(Match match) {
return Convert.ToChar(int.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber)).ToString();
}
public static string Decode(string str)
{
return Regex.Replace(str.Replace('+', ' '), "%[0-9a-zA-Z][0-9a-zA-Z]", new MatchEvaluator(DecodeEvaluator));
}
}
使い
System.Web.HttpUtility.urlencode("url")
を確認してください。ウェブの一つとして参照するには、次のようにしますとは思わないで参考としてデフォルトでコンソール。
は受け付けていませんUrlEncode法のHttpUtilityクラスです。
またこの問題を自分自身ではなくを追加します。Web組み立てる自分自身のプロジェクトを書いて、クラスの符号化/復号Url(そのインターネットはモバイルが、もちろん、一試験がないます。私はソースコードです。ください:のコメントのた場合は先頭のユこのせいにしない場合はやりとりするいわゆる"学びながらのコードです。
''' <summary>
''' URL encoding class. Note: use at your own risk.
''' Written by: Ian Hopkins (http://www.lucidhelix.com)
''' Date: 2008-Dec-23
''' </summary>
Public Class UrlHelper
Public Shared Function Encode(ByVal str As String) As String
Dim charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*'()"))
Dim pattern = String.Format("[^{0}]", charClass)
Dim evaluator As New MatchEvaluator(AddressOf EncodeEvaluator)
' replace the encoded characters
Return Regex.Replace(str, pattern, evaluator)
End Function
Private Shared Function EncodeEvaluator(ByVal match As Match) As String
' Replace the " "s with "+"s
If (match.Value = " ") Then
Return "+"
End If
Return String.Format("%{0:X2}", Convert.ToInt32(match.Value.Chars(0)))
End Function
Public Shared Function Decode(ByVal str As String) As String
Dim evaluator As New MatchEvaluator(AddressOf DecodeEvaluator)
' Replace the "+"s with " "s
str = str.Replace("+"c, " "c)
' Replace the encoded characters
Return Regex.Replace(str, "%[0-9a-zA-Z][0-9a-zA-Z]", evaluator)
End Function
Private Shared Function DecodeEvaluator(ByVal match As Match) As String
Return "" + Convert.ToChar(Integer.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber))
End Function
End Class
使用 WebUtility.UrlEncode(string)
から System.Net
名前空間
Kibbeeには、実際の答えです。あHttpUtility.UrlEncodeの方法に使用させることはできないすることで利用できるようになりのデフォルトコンソール-アプリケーション.す 必要 追加の参照。ます。これを実行するには、
- ごソリューションエクスプローラを右クリックし[参考文献
- を選択"追加"参考"
- の追加"参照"ダイアログボックスを使用。純タブ
- スクロールして、システム。ウェブを選択し、ヒットok
今で利用できますUrlEncode方法です。残っていを追加したい,
利用システム。ウェブ
上部のコンソールアプリ利用の名前を呼ぶときの通の方法
システム。ます。HttpUtility.UrlEncode(someString)
HttpUtility.UrlEncode("url"のとき)ます。ます。
利用の静的HttpUtility.UrlEncode方法です。
では、追加参照System.web..dll
-利用 var EncodedUrl=。ます。HttpUtility.UrlEncode("URL_TEXT");
きのファイル System.web.dll
Uriです。EscapeUriStringなどに用いるべきではありません脱出しに渡される文字列はURLでないエンコードのすべての文字とします。の'+'は良い例ではない自動的にエスケープされます。こしに変換スペースのURLはどのような意味で簡単にURIです。明らかに原因となる膨大な課題の分だそうとパスのようなものをベース64符号化された文字列のURL空間がすべてだという文字列を受けます。
利用できるHttpUtility.UrlEncodeを追加します参照プロジェクト(がんとの通信ウェブアプリケーション後であってはならない理由はなりません。います。
代替方法として使用することUriです。EscapeDataString間Uriです。EscapeUriStringで説明したもの。 https://stackoverflow.com/a/34189188/7391