【问题标题】:Retrofit getting 400 bad request in local iis server but 200 in post man改造在本地 iis 服务器中收到 400 个错误请求,但在邮递员中收到 200 个错误请求
【发布时间】:2018-07-23 05:20:13
【问题描述】:

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

但是当我尝试使用 retrofit2 从 andorid 请求时,我收到错误 400。

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


}

【问题讨论】:

  • 从 Auth @POST("/Auth/authenticate") 中删除 /
  • @Pavya 仍然无法正常工作
  • 在邮递员中,您还添加了标题检查
  • @Pavya 这是邮递员默认标题我没有添加任何内容
  • 你在你的服务器上尝试使用邮递员??

标签: android iis retrofit2 asp.net-core-2.0


【解决方案1】:

您应该明白的是,Android 模拟器(一个虚拟设备)有自己的网络。所以谷歌定义了一个神奇的IP地址“10.0.2.2”。如果您尝试浏览到此 IP 地址,请求将被路由到主机的“127.0.0.1”环回适配器。但是,默认情况下,这种魔法不适用于 IIS Express。

  1. 首先,将您的 192.168.0.104 地址替换为 END_POINT 中的 10.0.2.2。

    public static final String END_POINT = "http://10.0.2.2:50111/api/";
    
  2. 下载Jexus Manager并安装。

  3. 单击此处并设置 IIS(检查您的 VS 版本)-Managing IIS Express Servers

  4. 转到编辑站点 -> 绑定

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

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

    • 这些是我的设置。请更改您的设置。

  2. 运行服务器。

Android 似乎将 Host 标头设置为“127.0.0.1”,以便 IIS Express 可以以这种方式处理请求。

更多信息-How to Let Android Emulator Access IIS Express

【讨论】:

    【解决方案2】:

    您的请求似乎有误,请尝试以下方式。

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

    【讨论】:

    • 请检查您的 API 与此 URL public static final String END_POINT = "192.168.0.104:50111/api";在邮递员中让我们知道它是否有效。
    • 收到错误请求 - 主机名无效
    • 请使用您在邮递员中使用的本地主机 URL。
    【解决方案3】:

    您的请求似乎有误,请尝试以下代码,

    改造客户端

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

    改装电话

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

    【讨论】:

    • 仍然获得 400
    • 我收到了您的问题。在您的邮递员中,我可以看到“api”,但在您的改造网址中没有“api”关键字。那么你能不能用@POST("api/Auth/authenticate") 改变你的END_POINT = "192.168.0.104:50111" 和@POST("Auth/authenticate")。
    • @AndroidTeam 这根本不是问题。
    【解决方案4】:

    您在邮递员中使用本地服务器,例如 localhost

    这意味着你必须用你的电脑在模拟器中运行你的项目。并且您的电脑和本地服务器电脑都应该在同一个网络中。

    如果您使用您的设备进行测试,那么您的本地服务器机器网络和您的测试设备的 wifi 网络应该在同一个网络中。

    【讨论】:

    • @Amir Hossain :已修复。
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2018-07-07
    相关资源
    最近更新 更多