使用URL HttpClient的问题,其中包括大括号 [英] HttpClient problem with URLs which include curly braces

查看:880
本文介绍了使用URL HttpClient的问题,其中包括大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用HttpClient的为我的Andr​​oid应用程序。在某些时候,我有来从远程位置的数据。下面是我怎么利用的HttpClient获得响应的片段。

I am using HttpClient for my android application. At some point, I have to fetch data from remote locations. Below is the snippet how I made use of HttpClient to get the response.

String url_s = "https://mydomain.com/abc/{5D/{B0blhahblah-blah}I1.jpg"; //my url string
DefaultHttpClient httpClient = new DefaultHttpClient();
response = httpClient.execute(new HttpGet(url_s));

它的工作原理,在大多数情况下,绝对没问题,但不能当在我的网址一些大括号是基本的字符串。堆栈跟踪显示我的花括号说无效字符的索引。 于是,我就从EN codeD URL创建URI。

It works absolutely fine in most cases but not when there is some curly braces in my url which is String basically. The stack trace shows me the index of curly braces saying Invalid character. So I tried to create URI from encoded URL.

URL url = new URL(url_s);
URI uri = url.toURI();
response = httpClient.execute(new HttpGet(uri));

这样做之后,我并没有得到所有从远程位置的结果。我工作围绕这个问题,通过更换大括号固定它

After doing so, i didn't get the result from remote location at all. I worked around the problem and fixed it by replacing the curly brace

  • 在{和%7B
  • 在}和%7D

但我不完全同意我的解决方案满足。有没有更好的办法呢?任何整洁,没有硬codeD像我这样的?

But I am not totally satisfy with my solution. Are there any better solutions? Anything neat and not hard-coded like mine?

推荐答案

严格的答案是,你不应该花括号中的网址

The strict answer is that you should never have curly braces in your URL

有效的URL的完整描述可以在 RFC1738 中找到

A full description of valid URL's can be found in RFC1738

相关部分的这个答案如下:

The pertinent part for this answer is as follows

不安全:

字符可以是不安全的一些原因。空间
  字符是不安全的,因为显著的空间可能会消失,
  当URL被转录或
微不足道的空间可能出台   排版或受到的文字处理程序处理。
  字符<因为它们被用​​作
和>是不安全   在周围自由文本网址分隔符;的引号()用于到
  划在某些系统中的URL。字符#是不安全的,应该   总是烯$ C $光盘,因为它是用在万维网和其它
  系统从一个片段/锚定标识符分隔的URL可能   遵循它。字符%是不安全的,因为它是用来对于
  其他字符进行编码。其他字符是不安全的,因为
  网关和其他传输代理已知有时修改
  这样的字符。这些字符{,},|,\,^,〜,
  [,]和`。

Characters can be unsafe for a number of reasons. The space
character is unsafe because significant spaces may disappear and
insignificant spaces may be introduced when URLs are transcribed or
typeset or subjected to the treatment of word-processing programs.
The characters "<" and ">" are unsafe because they are used as the
delimiters around URLs in free text; the quote mark (""") is used to
delimit URLs in some systems. The character "#" is unsafe and should always be encoded because it is used in World Wide Web and in other
systems to delimit a URL from a fragment/anchor identifier that might follow it. The character "%" is unsafe because it is used for
encodings of other characters. Other characters are unsafe because
gateways and other transport agents are known to sometimes modify
such characters. These characters are "{", "}", "|", "\", "^", "~",
"[", "]", and "`".

所有不安全的字符必须始终连接的URL中的codeD。对于
  例如,#字符必须是EN codeD甚至在
URL中   不正常处理片段或锚
系统   标识符,这样,如果该URL被拷贝到另一个的系统,
  不使用它们,就没有必要改变URL编码

All unsafe characters must always be encoded within a URL. For
example, the character "#" must be encoded within URLs even in
systems that do not normally deal with fragment or anchor
identifiers, so that if the URL is copied into another system that
does use them, it will not be necessary to change the URL encoding.

为了绕过你已经遇到的问题,你必须连接code你的网址。

In order to bypass the problem you have been experiencing you must encode your url.

您遇到的问题主机不能为空的错误,当整个URL进行连接codeD包括 HTTPS:/ /mydomain.com/ 一部分,所以它被混淆。你只需要连接code中的URL的最后一部分称为路径。

The problem you experienced with the "host may not be null" error will happen when the entire url is being encoded including the https://mydomain.com/ part so it gets confused. You only want to encode the last part of the URL called the path.

解决的办法是使用Uri.Builder类从各个部分建立你的URI应该EN code的过程中路径

The solution is to use the Uri.Builder class to build your URI from the individual parts which should encode the path in the process

您会发现在的Andr​​oid SDK Uri.Builder参考文档详细说明

用你的价值观一些简单的例子是:

Some trivial examples using your values are:

Uri.Builder b = Uri.parse("https://mydomain.com").buildUpon();
b.path("/abc/{5D/{B0blhahblah-blah}I1.jpg");
Uri u = b.build();

或者你也可以使用链接:

Or you can use chaining:

    Uri u = Uri.parse("https://mydomain.com").buildUpon().path("/abc/{5D/{B0blhahblah-blah}I1.jpg").build();

这篇关于使用URL HttpClient的问题,其中包括大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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