iS_Tom
iS_Tom

Reputation: 11

Error 403 when sending the request to the server ASP.NET Web Api

I do customer authorization Android using ASP.NET Web API Web server. I enter my login name, press enter, get an answer the user is found. But it is only through the android emulator. When I try on a real device, the error 403 port on the router and open on your computer, too. Telephone and computer on the same network. Page which is on that server opens in your phone browser if u drive a computer and the port (192.168.0.101:7899). Here is a request to the server:

@GET("/login")
void findLogin(@Query("login") String login, Callback<Users> callback);

That connection code:

 public class RestClient {
       private UploadService  uploadService;
       private String URL ="http://192.168.0.101:7899/api/";       
       public RestClient(){
             Gson localGson = new GsonBuilder().create();
             this.uploadService = ((UploadService)new RestAdapter.Builder()
                 .setEndpoint(URL)
                 .setConverter(new GsonConverter(localGson))
                 .setRequestInterceptor(new RequestInterceptor() {
                            public void intercept(RequestFacade requestFacade) {
                                if (URL.contains("192.168.0.101")) {                             
                                    requestFacade.addHeader("Host", localhost");
                                }
                            }
                        })
                .build().create(UploadService.class));    
        }

How to solve the problem with the error 403? p.s sorry for my bad english

Upvotes: 0

Views: 386

Answers (1)

Chintan Soni
Chintan Soni

Reputation: 25267

Assuming that you are facing issue of CORS.

So, first we need to import those packages using NuGet Package Manager. Open Package Manager Console by clicking on Tools > NuGET Package Manager > Package Manager console, there you simply enter the following command:

Install-Package Microsoft.AspNet.WebApi.Cors –IncludePrerelease

Now, In Solution Explorer, go to App_Start > WebApiConfig.cs Add the below lines in Register(…) method:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;

And

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

So, your WebApiConfig.cs would look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;
namespace Student_Management_System
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;
            // Web API configuration and services
            var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);
            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });
        }
    }
}

You might want to refer my blog: https://programmingwithease.wordpress.com/2014/06/18/learning-asp-net-web-api-2-using-c/

Upvotes: 1

Related Questions