【问题标题】:link working in browser but not in HttpUrlConnection链接在浏览器中有效,但在 HttpUrlConnection 中无效
【发布时间】:2017-01-17 11:11:17
【问题描述】:

public class DPLoader extends AsyncTask<String, Void, String> {

    Context context;


    DPLoader(Context context) {
        this.context = context;
    }


    @Override
    protected String doInBackground(String... params) {


        String login_url = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

            try {

                URL url = new URL(login_url);
                HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
                httpurlconnection.setDoInput(true);

// 下面我尝试阅读但没有得到结果

InputStream inputstream = httpurlconnection.getInputStream();
                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream, "iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedreader.readLine()) != null) {
                    result += line;
                }
                bufferedreader.close();
                inputstream.close();
                httpurlconnection.disconnect();

                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        return "null";

    }


    @Override
    protected void onPreExecute() {
       // Toast.makeText(context, ""+ usernameEt.getText().toString(), Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void onPostExecute(String result) {
       Toast.makeText(context, ""+result, Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

}

image 是我用httpurlconnection.getErrorstream()`` and withhttpurlconnection.getInputStream() 尝试的结果吐司,我得到了空值。 当我在浏览器中打开链接时,它会回显结果,但不会在 android httpurlconnection 中回显结果

  1. 列表项

【问题讨论】:

    标签: android android-networking androidhttpclient


    【解决方案1】:

    错误表明您无权访问您请求的资源。使用 HttpUrlConnection 拨打电话时需要传递登录名/密码。

    它在浏览器中工作的原因很可能是您已经在浏览器中登录了。

    【讨论】:

    • 我没有在浏览器中登录此链接,但此链接在浏览器中有效以回显结果,但在 adnroid 中无效
    • 你能告诉我什么是响应码吗?你可以得到它如下:httpurlconnection.getResponseCode()
    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多