1

我想使用 https(具有传输安全性的 webHttpBinding)连接到托管在 windows azure 上的 wcf json 服务。地址是 some_subdomain.somesite.com,当然会重定向到 someapp.cloudapp.net 我不能使用“some_subdomain.somesite.com”,因为主机名中有“_”。

原始代码

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serviceUrl + "/" + serviceMethod);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
String jsonParameters = gson.toJson(parameters);
post.setEntity(new StringEntity(jsonParameters));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity(); // throws here
return convertStreamToString(entity);

抛出 java.lang.IllegalArgumentException:主机名不能为空;

这个类似的代码:

        InputStream inputStream;            
        URL url = new URL(serviceUrl + "/" + serviceMethod);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("GET");
        httpConn.connect(); // throws exception here
        if(httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            inputStream = httpConn.getInputStream();
            ...
        }

抛出 NullPointerException。

如果我只是输入IP,那么我得到

javax.net.ssl.SSLException:证书中的主机名不匹配:IP_ADDRESS!= some_subdomain.somesite.com

我正在使用生产证书,因此更改域并获取另一个证书可能不是一种选择。请注意,在 iOS 和 WinRT 版本上一切正常,只有 java 似乎有主机名问题。

那么我该如何:a)在主机名中使用带有“_”的 URL 或 b)配置服务器的预期身份?

4

1 回答 1

2

您不能在主机名中添加下划线,请参见此处此处

于 2012-10-02T11:58:02.547 回答