停止逃离字符串的webclient [英] Stop webclient from escaping strings

查看:108
本文介绍了停止逃离字符串的webclient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Google Maps API来进行地理编码/反向地理编码坐标/地址。为此,我使用了C#的webclient类实例。

  static string gMapsUrl =http://maps.googleapis。 com / maps / api / geocode / xml?address = {0}& sensor = false; 
public static List< Location> RetrieveCoordinate(string address)
{
string requestUri = string.Format(gMapsUrl,address);
string result = string.Empty;

使用(var client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
result = client.DownloadString(requestUri);
}
....
}

这通常适用,但是可以说我想对地址Götznerstraße进行反向地理编码。如果我在浏览器中手动执行此操作,则一切正常,请求的URL将为

  http:// maps。 googleapis.com/maps/api/geocode/xml?address=götznerstraße&sensor=false

最终的请求从我的程序看起来像这样

  GET / maps / api / geocode / xml?address = G%C3%B6tznerstra% C3%9Fe& sensor = false HTTP / 1.1 

这会导致Google找不到匹配项。对我来说,看起来好像webclient在某个地方逃跑了,这阻止了我获得结果。有没有办法停止这样做的webclient,或让谷歌unescape字符串再次?

编辑:
我解决了它,比较来自浏览器和我的程序,我意识到我的浏览器正在发送一些额外的头文件。更改的实现如下所示:

  using(var client = new WebClient())
{
client .Headers.Add(Accept,text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,* / *; q = 0.8);
client.Headers.Add(Accept-Encoding,gzip,deflate,sdch);
client.Headers.Add(Accept-Language,de-DE,de; q = 0.8,en-US; q = 0.6,en; q = 0.4);
client.Encoding = System.Text.Encoding.UTF8;
result = client.DownloadString(requestUri);
}

非常感谢所有帮助过我的人!

解决方案

它必须转义url。然而,没有什么能够阻止你使用 HttpUtility.UrlDecode 来隐藏它。
也可以看看这里获取更深入的答案。


I want to use the Google Maps API to geocode/reverse-geocode coordinates/addresses. To do this, I use an instance of C#'s webclient class.

static string gMapsUrl= "http://maps.googleapis.com/maps/api/geocode/xml?address=  {0}&sensor=false";
public static List<Location> RetrieveCoordinate(string address)
{
    string requestUri = string.Format(gMapsUrl, address);
    string result = string.Empty;

    using (var client = new WebClient())
    {
        client.Encoding = System.Text.Encoding.UTF8;
        result = client.DownloadString(requestUri);
    }
....
}

This usually works, however lets say I want to reverse-geocode the address "Götznerstraße". If I do this manually in a browser, everything works fine, the URL of the request would be

http://maps.googleapis.com/maps/api/geocode/xml?address=götznerstraße&sensor=false

The eventual request from my program however looks like this

GET /maps/api/geocode/xml?address=G%C3%B6tznerstra%C3%9Fe&sensor=false HTTP/1.1 

This leads to Google finding no matches. To me it appears as if the webclient escapes the umlaute somewhere, which prevents me from getting results. Is there a way to stop the webclient from doing this, or make Google unescape the string again?

EDIT: I solved it, after comparing the request from the browser and my program, I realized that my browser was sending some additional headers. The changed implementation looks like this

using (var client = new WebClient())
{
    client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    client.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
    client.Headers.Add("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4");
    client.Encoding = System.Text.Encoding.UTF8;
    result = client.DownloadString(requestUri);
}

Thanks a lot to everyone that helped!

解决方案

It has to escape the url. However there is nothing stopping you from using HttpUtility.UrlDecode to unescape it. Also have a look here for a more indepth answer.

这篇关于停止逃离字符串的webclient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆