使用httpurlconnection时具有波浪号的站点的FileNotFoundException

时间:2014-11-08 15:13:15

标签: android post character-encoding httpurlconnection

使用代字号将某些数据发布到网站时出现了一个奇怪的问题。 我怎么能在Android中修复它?

例如,这是我的网站: http://host-site.com/~username/form.php

这是我的代码:

url = new URL(hostSiteString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Host", "myHost");
urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0");
urlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
urlConnection.setRequestProperty("Accept-Language", "en-US;q=0.7,en;q=0.3");
urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
urlConnection.setRequestProperty("Connection", "keep-alive");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setDoOutput(true);

BufferedOutputStream bos = new BufferedOutputStream(urlConnection.getOutputStream());
bos.write(readyToken.getBytes(), 0, readyToken.length());
bos.flush();
bos.close();

1 个答案:

答案 0 :(得分:0)

找到解决方案,我不得不解码我的网址。

String decode = URLDecoder.decode(hostSiteString);
url = new URL(decode);