Android的HTTP请求奇怪404未找​​到问题 [英] Android HTTP request wierd 404 not found issue

查看:3646
本文介绍了Android的HTTP请求奇怪404未找​​到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想打,我就用下面的code一个网址

In my application, I am trying to hit a URL which I do using the following code

  try {
    url = new URL(serverURL);

    httpURLConnection = (HttpURLConnection) url.openConnection();

    int timeout = 30000;
    httpURLConnection.setConnectTimeout(timeout);
    httpURLConnection.setReadTimeout(timeout);

    httpURLConnection.connect();

    String httpResponseMessage = httpURLConnection.getResponseMessage();
    responseCode = httpURLConnection.getResponseCode();

    Log.i(LOG_TAG,"Response code "+responseCode);

    } catch (Exception e) {
    e.printStackTrace();
    }

在通过浏览器中打开(电脑以及手机上)的(保密)网址,完美的作品和响应预期。但是,当我通过上面这段code击中同一个URL,它给了我回应code 404(未找到)。谁能告诉我的问题可能是什么?
(对不起,不能因为是高度机密的发布网址。)

The (confidential) URL when opened through browser (on computer as well as on phone), works perfectly and the response is as expected. But when I hit the same URL via the above piece of code, it gives me response code 404 (NOT FOUND). Can anybody tell me what the issue can be? (Sorry, can not post the URL since is highly confidential.)

推荐答案

问题解决了:)

      try {

            url = new URL(serverURL);

            Log.i(LOG_TAG, url+"");
            HttpGet method= new HttpGet(new URI(serverURL));
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(serverURL));
            HttpResponse response = client.execute(method);
            responseCode = response.getStatusLine().getStatusCode();

            Log.i(LOG_TAG,"Response code response "+response);
            Log.i(LOG_TAG,"Response responseCode "+responseCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

这篇关于Android的HTTP请求奇怪404未找​​到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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