httpClient.execute更改传递的主机名

时间:2015-04-08 17:38:14

标签: java android

我有一个Android应用程序,它将http请求发送到作为服务运行的自托管API应用程序。

我遇到一个问题,即httpClient.execute(httpRequest)尝试解析与传递的主机名不同的主机名,它实际上删除了网址中的部分IP地址。
我需要做些什么来确保我的代码解析为正确的IP?

我尝试在请求标题中添加'no-cash'和'no-store',我还清除了平板电脑上的应用程序现金,但仍然会继续发生。我正在使用带有4.2.2 android版本的Samsung Tab 2。

这是我的代码:

    try 
    {

        Log.i("RESTfulClientServiceResponse.Get", "url is: " + url);

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpRequest = new HttpGet(url);
        httpRequest.setHeader("Content-Type", "application/json");
        httpRequest.setHeader("Accept", "application/json");
        //httpRequest.setHeader("Cache-Control", " max-age=0, no-cache, no-store");

        HttpResponse response = httpClient.execute(httpRequest);


        stream = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer buffer = new StringBuffer("");
        String line = "";
        String newLine = System.getProperty("line.separator");

        while ((line = stream.readLine()) != null)
        {
            buffer.append(line + newLine);
        }

        stream.close();

        return new RESTfulClientServiceResponse(response.getStatusLine().getReasonPhrase(), buffer.toString());
    } 
    catch (Exception ex) 
    {
        if (ex.getMessage() != null)
        {
            Log.i("RESTfulClientService.GET method", ex.getMessage());
        }
        else
        {
            ex.printStackTrace();
        }
        throw new RESTfulClientServiceException(String.format("Error getting from url %s", url), ex);
    } 

代码错误在行

HttpResponse response = httpClient.execute(httpRequest); 

并且异常“无法解析主机....”,但主机与传递的内容不同。我逐步完成了代码,无法找到想要解决的错误主机。

这是来自logcat的日志:

I/RESTfulClientServiceResponse.Get(31355): url is: http://123.456.7.890:6005/dispatcher/statuscheck
D/libEGL(31355): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
D/libEGL(31355): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
D/libEGL(31355): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
D/OpenGLRenderer(31355): Enabling debug mode 0
D/ProgressBar(31355): updateDrawableBounds: left = 0
D/ProgressBar(31355): updateDrawableBounds: top = 0
D/ProgressBar(31355): updateDrawableBounds: right = 48
D/ProgressBar(31355): updateDrawableBounds: bottom = 48
D/dalvikvm(31355): threadid=11: still suspended after undo (sc=1 dc=1)
04-08 12:39:49.210: I/RESTfulClientService.GET method(31355): Unable to resolve host "123.456.890": No address associated with hostname

我用logog log 123.456.7.890替换了logcat日志中的真实IP地址,但它正在发生的是从第3部分删除它。

0 个答案:

没有答案