尝试使用REST Web服务时获取403(禁止)

时间:2014-05-17 11:16:58

标签: php android rest

我尝试在我的Android应用程序中使用REST Web服务,并且我总是在状态代码中获得403。 Web服务由安装在Wampserver上的Web应用程序提供。我尝试使用的方法称为glpi.doLogin,它带有两个prams用户名和密码。它在我的浏览器上工作正常。这是代码:

package com.example.ws;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

        HttpClient client;
        HttpGet httpGet;
        HttpResponse reponce;
        public Toast toast;
        public static final String TAG ="Mon_TAG";
        public TextView letexte;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            letexte= (TextView) findViewById(R.id.letexte);

            Button btn = (Button) findViewById(R.id.MyBtn);
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    c();

                }
            });
        }

        public void c(){

            client = new DefaultHttpClient();
            httpGet = new HttpGet("http://169.254.101.101:80/plugins/webservices/rest.php?method=glpi.doLogin&login_name=glpi&login_password=glpi"); 





            Thread t = new Thread(new t());

            t.start();

            if(reponce != null){

                try{


                    StatusLine status = reponce.getStatusLine();
                    int codeStatus = status.getStatusCode();
                    if(codeStatus != 200){
                        String su = "status code = "+ codeStatus;
                        update(su);

                    }

                    else{

                        HttpEntity corps = reponce.getEntity();
                        InputStream is = corps.getContent();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        StringBuilder s = new StringBuilder();
                        String r;
                        while((r = br.readLine()) != null){

                            s.append(r);
                        }

                        String stringFinal = s.toString();


                        update(stringFinal);
                    }

                }
                catch(IOException e){
                    String s = "Error cnx";
                    update(s);
                }
            }

            else update("reponce null");
        }




        public void update(String s){

            letexte.setText(s);
        }

        class t implements Runnable {
            public void run(){
                try{


                    reponce = client.execute(httpGet);

                }
                catch(IOException e){
                    Log.d("Thread","Error");
                    e.printStackTrace();


                }

            }
        }
}

0 个答案:

没有答案