0

I have this code, who should connect to a php remote file and should get a String representing a XML file. But something is wrong, it is giving me error 401.

The variable url is the direction of the php:

String response=getXML("http://ficticiousweb.com/scripts/getMagazinesList.php");

If i paste the real direction (that is a ficticious direction) on the webbrowser, it works and gives me the XML.

This is my code:

public String getXML(String url){
    try{
        StringBuilder builder = new StringBuilder();
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        int statuscode = response.getStatusLine().getStatusCode();
        if(statuscode == 200)
        {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null)  builder.append(line);               
        }
        else throw new Exception("HTTP error: " + String.valueOf(statuscode));
        return builder.toString();
    }catch(Exception e){e.printStackTrace();}
    return null;
}

What is wrong with the code?

thanks

2
  • 401 is "unauthorized". The server is asking you for authentication. You just need to provide those authentication details (probably in an Authorization: HTTP header) and it should work...
    – DaveRandom
    Commented Mar 15, 2012 at 11:22
  • ooops, i have a pass and a user, but i dont know how to put them with java code, can you tell me how? btw the webbrowser isn't asking me for authorization :S Commented Mar 15, 2012 at 11:23

2 Answers 2

1

You need to login to the requested site in order to download or access the xml. This can be done by authenticated schema based upon what is supported. Normally, there are 2 types of schemas where used. Basic and Digest. Below code will help you for BASIC AUTH.

HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String _username = "username";
    String _password = "password";
    try {
         ((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(
                new org.apache.http.auth.AuthScope(webhostname, webport)),
                new org.apache.http.auth.UsernamePasswordCredentials(_username, _password));

        response = httpclient.execute(new HttpGet(completeurlhere));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK) {
            try {
                InputStream is = response.getEntity().getContent();
                this._data = is;

            } catch(Exception ex) {
                Log.e("DBF Error",ex.toString());
            }                
        } else {
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch(ClientProtocolException cpe) {
        Log.e("ClientProtocolException @ at FPT",cpe.toString());
    } catch(Exception ex) {
        Log.e("Exception at FETCHPROJECTASK",ex.toString());
    }
4
  • What should i put in webhostname and webport on the line: " new org.apache.http.auth.AuthScope(webhostname, webport)), " ???? Commented Mar 15, 2012 at 11:37
  • webhost means the name of your website "ficticiousweb.com". and if your protocol is "http" then it means port is "80" else if protocol is "https" then port is 443. Commented Mar 15, 2012 at 11:47
  • i tryed with "null",-1 and it works. I put my the complete URL on completeurlhere variable and it works. I'm doing in the correct way? Commented Mar 15, 2012 at 11:58
  • yes! make a Log.i("DATA RECIEVED",is.toString()); to check that, you're getting yourXML in response or not. This code will work for you effectively if "BASIC" auth is used. Commented Mar 15, 2012 at 12:11
0

Well a 401 means you aren't Authorized to do the GET request. You should ask the website how to Authenticate the request...

Authorization happens through the Authorization Header in the HTTP request. You should look into that and probably fill that header yourself with your credentials... (if the server accepts that)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.