2

I'm trying to use emulator to connect to the .net Web API

But the emulator always response 405 Method Not Allow

Here is my android code on eclipse

String url1 = "http://192.168.1.106:8085/api/user";
                con = (HttpURLConnection) ( new URL(url1).openConnection());
                con.setRequestMethod("GET");
                con.setDoInput(true);
                con.setDoOutput(true);
                con.connect();

here is the url run on browser http://192.168.1.106:8085/api/user

[{"userIDs":"asasasas","pwd":"asasas"},{"userIDs":"jacky","pwd":"jacky"},{"userIDs":"danzo","pwd":"danzo"},{"namIDs":"abcdef","pwd":"abc123"}]

here is my api controller on visual studio 2013

public class userController : ApiController
{
    [HttpGet]
    public List<userlist> Get()
    {

        var mylist = new List<userlist> { };
        var mylist1 = new List<userlist> { };
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            string query = "SELECT UserName, password FROM UserProfile, webpages_Membership WHERE  UserProfile.UserId = webpages_Membership.UserId;";
            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var result = new userlist();
                    result.namIDs = reader.GetString(0);
                    result.pwd = reader.GetString(1);
                    mylist.Add(result);
                }

                reader.Close();
            }

            connection.Close();
        }

        return mylist;
    }

    public class userlist
    {
        public string userIDs { get; set; }
        public string pwd { get; set; }
    }

}

I have tried to view all the similar question

and try to put <remove name="WebDAV" />

and try to put [HttpGet]

But still not work to me

w.c.nm
  • 61
  • 5

1 Answers1

0

I have found an solution to solve this

which is change the Http Url connection to HttpClient

HttpClient httpclient = new DefaultHttpClient();

                    // make GET request to the given URL
                    HttpResponse httpResponse = httpclient.execute(new HttpGet(url1));

                    // receive response as inputStream
                    inputStream = httpResponse.getEntity().getContent();

Then I get it !!

w.c.nm
  • 61
  • 5