问题将XML发布到子域具有连字符的URL

时间:2012-02-27 11:05:29

标签: android xml post subdomain hyphen

我在发布到网络服务时遇到了实际问题,似乎问题出在网址上 - 子网域中有一个连字符。下面的网址不是真正的网址,但您应该明白这一点。

在传递带有连字符的URL时出现此错误:

02-27 10:33:45.992:E / AndroidRuntime(2226):java.lang.IllegalArgumentException:主机名不能为空

如果省略连字符,那么至少它会查找URL。

请帮忙!

    HttpClient httpclient = new DefaultHttpClient();

    try {
        HttpPost httppost = new HttpPost("http://mbhh.one-dev.co.uk/HandsetService.asmx?op=Opp");

        StringEntity se = new StringEntity( getUploadXml ().toString(), HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setEntity(se);

        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity resEntity = httpresponse.getEntity();
        String result = EntityUtils.toString(resEntity);
        Log.d(TAG, "writer = "+result);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:1)

您可以使用URI constructor自动转义无效字符:

URL url = new URI("http", "//mbhh.one-dev.co.uk/HandsetService.asmx?op=Opp", null).toURL();