Retrofit getting 400 bad request in local iis server but 200 in post man

醉酒当歌 提交于 2020-05-17 07:49:07

问题


I have local asp .net core 2 IIS server. I am getting result when I am using post request.

But I am getting error 400 when I am trying to request from andorid using retrofit2.

My android device and server are connected to same wifi network

postman returns

retrofit return

I am using my server pc ip instead of localhost for retrofit because retrofit does not support local host

retrofit client

@POST("/Auth/authenticate")
Call<ApiResponse> createAccount(@Body Request user);

retrofit call

    request = new Request("01532383497", "123");

    retrofit = RetrofitInstance.getInstance();

    client = retrofit.create(APIClient.class);

    client.createAccount(request).enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
            String a=response.body().getType();
            Log.d("d","d");
        }

        @Override
        public void onFailure(Call<ApiResponse> call, Throwable t) {
            Log.d("d","d");
        }
    });

Request object

public class Request {

String Username;
String Password;

public Request(String Username, String Password) {
    Username = Username;
    Password = Password;
}

public String getUsername() {
    return Username;
}

public void setUsername(String username) {
    Username = username;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
}
}

retrofit instance

public class RetrofitInstance {

public static Retrofit retrofit = null;
public static final String END_POINT = "http://192.168.0.104:50111/api/";

public static Retrofit getInstance(){
    if (retrofit == null){

        retrofit = new Retrofit.Builder().baseUrl(END_POINT)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }
    return retrofit;
}


}

回答1:


You should understand is that the Android emulator (a virtual device) has its own networking. So Google defines a magic IP address “10.0.2.2”. If you try to browse to this IP address, the requests would be routed to “127.0.0.1” loopback adapter of the host machine. However, this magic won’t work for IIS Express by default.

  1. First, replace your 192.168.0.104 address to 10.0.2.2 in END_POINT.

    public static final String END_POINT = "http://10.0.2.2:50111/api/";
    
  2. Download Jexus Manager and install.

  3. Click here and set up the IIS(Check with your VS version) - Managing IIS Express Servers.

  4. Go to Edit site -> bindings

  1. Add new binding with correct values and the Hostname should be 127.0.0.1.

    • In my case, my access point from the local - https://localhost:44317/api
    • after the add new binding, it shows as,

    • These are my settings. please change with your settings.

  2. Run the server.

It seems that the Android magic sets Host header to “127.0.0.1” so that IIS Express can process the requests in this way.

For further information - How to Let Android Emulator Access IIS Express




回答2:


Seems your request is wrong please try below way.

@POST("Auth/authenticate")
Call<ApiResponse> createAccount(@Field("Username") String username, @Field("Password") String password);



回答3:


It looks like your request is wrong, Try below code,

retrofit client

@POST("Auth/authenticate")
@FormUrlEncoded
Call<ApiResponse> createAccount(@FieldMap Map<String,String> params);

retrofit call

Map<String,String> params = new HashMap<String, String>();
    params.put("Username", "01532383497");
    params.put("Password", "123");

retrofit = RetrofitInstance.getInstance();

client = retrofit.create(APIClient.class);

client.createAccount(params).enqueue(new Callback<ApiResponse>() {
    @Override
    public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
        String a=response.body().getType();
        Log.d("d","d");
    }

    @Override
    public void onFailure(Call<ApiResponse> call, Throwable t) {
        Log.d("d","d");
    }
  });



回答4:


You are using local server in postman like localhost.

It means you have to run your project in Emulator with your pc. and your pc and local server pc both should be in same network.

And if you are test with your device then your local server machine network and your wifi network of your testing device should be in same network.



来源:https://stackoverflow.com/questions/51471975/retrofit-getting-400-bad-request-in-local-iis-server-but-200-in-post-man

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!