System.Web.HttpUtility.UrlEncode方法使用不同的语言值给出错误的结果

时间:2015-04-25 11:51:12

标签: asp.net-mvc-4 c#-4.0

我项目中的Web.HttpUtility.UrlEncode方法。当我用英语编码名字时,我得到了正确的结果。例如,

string temp = System.Web.HttpUtility.UrlEncode("Jewelry");

然后我在temp变量中得到了确切的结果。但如果我用俄语写名字,那么我会得到不同的结果。

string temp = System.Web.HttpUtility.UrlEncode("ювелирные изделия"); 

然后我得到临时变量的值,如"%d1%8e%d0%b2%d0%b5%d0%bb%d0%b8%d1%80%d0%bd%d1%8b%d0% B5 +%D0%B8%D0%B7%D0%B4%D0%B5%D0%BB%D0%B8%D1%8F"

任何人都可以帮助我如何根据语言获得确切的名称吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

实际上,该方法已为您“做了正确的事”!

它对非ASCII字符进行编码,使其在所有情况下都有效并通过Internet进行传输。如果您将Set变量作为参数放在URL中,您将在服务器端获得正确的结果。这就是UrlEncode的意思。在这里你的问题根本不是问题。 因此,请查看此链接以进一步阅读以了解有关URL编码的信息:How to make a redirect in PHP?

如果您将俄语单词输入到我给出的页面中的“URL编码功能”部分,它将返回与new HashSet<>(map.values())方法相同的结果。

答案 1 :(得分:0)

  

任何人都可以帮助我如何根据语言获得确切的名称吗?

简而言之:不是采用这种方法,但可能取决于你的确切目标。

详情:

通常,RFC 3986定义的URI(请参阅Section 2: Characters)可能包含以下任何字符:from gi.repository import Gtk, Gdk import json import random class LayerTreeView(Gtk.TreeView): def __init__(self, store): Gtk.TreeView.__init__(self, store) renderer = Gtk.CellRendererText() column = Gtk.TreeViewColumn("Name", renderer, text=0) self.append_column(column) class DataTreeView(Gtk.TreeView): def __init__(self, store): Gtk.TreeView.__init__(self, store) self.store = store renderer = Gtk.CellRendererText() column = Gtk.TreeViewColumn("Data", renderer, text=0) self.append_column(column) class MainWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="TreeView Serialize") self.connect("delete-event", Gtk.main_quit) self.set_border_width(10) self.set_default_size(400, 300) vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6, expand=True) self.add(vbox) self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6) button = Gtk.Button("Cut") button.connect("clicked", self.on_cut_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button(stock=Gtk.STOCK_COPY) button.connect("clicked", self.on_copy_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button(stock=Gtk.STOCK_PASTE) button.connect("clicked", self.on_paste_clicked) hbox.pack_start(button, True, True, 0) vbox.add(hbox) self.layer_store = Gtk.TreeStore(str, object, object) self.layer_view = LayerTreeView(self.layer_store) self.layer_sw = Gtk.ScrolledWindow() self.data_sw = Gtk.ScrolledWindow() self.layer_sw.add(self.layer_view) treebox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6, expand=True) treebox.pack_start(self.layer_sw, True, True, 0) treebox.pack_start(self.data_sw, True, True, 0) vbox.add(treebox) self.select = self.layer_view.get_selection() self.select.connect("changed", self.on_selection_changed) self.add_test_data() def add_test_data(self): for x in range(3): data_store = Gtk.ListStore(str) data_view = DataTreeView(data_store) for y in range(5): data_store.append([str(y+x)]) self.layer_store.append(None, ["Data {}".format(x), data_store, data_view]) def on_selection_changed(self, selection): """ When layer is switched load respective data """ model, treeiter = selection.get_selected() if treeiter != None: data_view = model[treeiter][2] child = self.data_sw.get_child() if child != None: self.data_sw.remove(self.data_sw.get_child()) self.data_sw.add(data_view) self.show_all() def on_cut_clicked(self, button): pass def on_copy_clicked(self, button): copy_list = ["safe-to-paste"] data_dict = {} for row in self.layer_store: name = row[0] data_obj = row[1] value_list = [] for datarow in data_obj: value = datarow[0] value_list.append(value) data_dict[name] = value_list copy_list.append(data_dict) data = json.dumps(copy_list) self.clipboard.set_text(data, -1) def on_paste_clicked(self, button): paste_str = self.clipboard.wait_for_text() try: parse = json.loads(paste_str) json_str = True except: json_str = False if json_str is False: return keyword = parse[0] if keyword != "safe-to-paste": return data_dict = parse[1] for x in data_dict: data_list = data_dict[x] data_store = Gtk.ListStore(str) data_view = DataTreeView(data_store) for y in data_list: data_store.append([str(y)]) self.layer_store.append(None, [x, data_store, data_view]) win = MainWindow() win.show_all() Gtk.main() 。任何其他字符都需要使用百分比编码(%hh)进行编码。

这就是UrlEncode产生

的原因
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=

UrlEncode("Jewelry") -> "Jewelry" UrlEncode("ювелирные изделия") -> "%d1%8e%d0%b2%d0%b5%d0%bb%d0%b8%d1%80%d0%bd%d1%8b%d0%b5+%d0%b8%d0%b7%d0%b4%d0%b5%d0%bb%d0%b8%d1%8f" 字符串包含根据RFC 3986在URL中不允许的字符。

今天,在网址中modern browsers could work with UTF-8可能没有必要使用UrlEncode()。请参阅示例:http://jsfiddle.net/ybgt96ms/