HTTP 错误 400。请求主机名无效 - 改造
Posted
技术标签:
【中文标题】HTTP 错误 400。请求主机名无效 - 改造【英文标题】:HTTP Error 400. The request hostname is invalid - Retrofit 【发布时间】:2020-04-09 04:23:54 【问题描述】:我正在 Visual Studio 2019 中运行一个 Web API 项目。它在我机器上的 localhost 上运行良好。当我发出 GET /POST 请求时,请求返回“400 bad requests invalid hostname”。
这是我的 Web API
[AllowAnonymous]
[HttpGet("forTest")]
public async Task<IActionResult> ForTest()
return Ok(true);
我可以在邮递员中访问服务器,调用https://localhost:44317/api/users/forTest
。
这是我的 android studio 设置,
API客户端
public class APIClient
public static final String BASE_URL = "https://10.0.2.2:44317/api/";
private static Retrofit retrofit = null;
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static Retrofit getClient()
if (retrofit == null)
//Defining the Retrofit using Builder
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL) //This is the only mandatory call on Builder object.
.addConverterFactory(GsonConverterFactory.create())
.client(getUnsafeOkHttpClient().build())
.build();
return retrofit;
public static OkHttpClient.Builder getUnsafeOkHttpClient()
try
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]
new X509TrustManager()
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[]
chain, String authType) throws CertificateException
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[]
chain, String authType) throws CertificateException
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers()
return new java.security.cert.X509Certificate[];
;
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier()
@Override
public boolean verify(String hostname, SSLSession session)
return true;
);
return builder;
catch (Exception e)
throw new RuntimeException(e);
ApiService
public interface ApiService
@POST("users/authenticate")
Call<List<AuthData>> doLogin(@Body AuthBody authBody);
@GET("users/forTest")
Call<Boolean> ApiTest();
主活动
Retrofit retrofit = APIClient.getClient();
ApiService apiService = retrofit.create(ApiService.class);
Call<Boolean> call = apiService.ApiTest();
call.enqueue(new Callback<Boolean>()
@Override
public void onResponse(Call<Boolean> call, Response<Boolean> response)
if(response.code() == 400)
try
Log.i("TAG", response.errorBody().string());
catch (IOException e)
e.printStackTrace();
Log.i("TAG", "ok");
@Override
public void onFailure(Call<Boolean> call, Throwable t)
Log.i("TAG", t.getLocalizedMessage());
);
响应为,
HTTP 错误 400。请求主机名无效。
有什么解决办法吗?
【问题讨论】:
blog.lextudio.com/… 天哪。这就是解决方案。谢谢@LexLi 【参考方案1】:根据@LexLi 的评论,我调查了问题并找出了解决方案。
原创文章-https://blog.lextudio.com/how-to-let-android-emulator-access-iis-express-f6530a02b1d3
【讨论】:
以上是关于HTTP 错误 400。请求主机名无效 - 改造的主要内容,如果未能解决你的问题,请参考以下文章
错误请求 - 从跨 LAN 访问本地主机 Web API 或 Web 应用程序时主机名无效
HTTP 400 错误 - 请求无效 (Bad request)
HTTP 400 错误 - 请求无效 (Bad request)