改造在本地IIS服务器中收到400个错误请求,但在邮递员中收到200个错误请求 [英] Retrofit getting 400 bad request in local iis server but 200 in post man

查看:111
本文介绍了改造在本地IIS服务器中收到400个错误请求,但在邮递员中收到200个错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有本地asp .net core 2 IIS服务器.我在使用发布请求时得到结果.

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

但是,当我尝试使用Retrofit2从Andorid请求时,出现了错误400.

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

我的android设备和服务器连接到同一个wifi网络

邮递员归来

改装退货

我正在使用服务器pc ip而不是localhost进行改造,因为改造不支持本地主机

改造客户

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

改造电话

    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");
        }
    });

请求对象

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;
}
}

改造实例

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;
}


}

推荐答案

您应该了解,Android模拟器(虚拟设备)具有自己的网络.因此Google定义了一个神奇的IP地址"10.0.2.2".如果尝试浏览到该IP地址,则请求将被路由到主机的"127.0.0.1"回送适配器.但是,默认情况下,这种魔术不适用于IIS Express.

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. 首先,将您的192.168.0.104地址替换为 END_POINT 中的10.0.2.2.

  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/";

  • 下载 Jexus Manager 并安装.

  • Download Jexus Manager and install.

    单击此处并设置IIS(请检查您的VS版本)-管理IIS Express服务器.

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

    转到编辑站点"->绑定

    Go to Edit site -> bindings

    1. 添加具有正确值的新绑定,并且主机名应为127.0.0.1.

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

    • 就我而言,我的访问点来自本地-https://localhost:44317/api
    • 添加新绑定后,显示为

    • 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.

    运行服务器.

    似乎Android魔术将Host标头设置为"127.0.0.1",以便IIS Express可以以这种方式处理请求.

    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.

    有关更多信息- 查看全文

  • 登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆