Java – use okhttp to get HTTP status code

I'm using okhttp to get some website content

However, I can't get the HTTP status code from the response

My java code:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
                    .url("https://www.google.at")
                    .build();
Response httpResponse = client.newCall(request).execute();    
String html = httpResponse.body().string();

This method:

httpResponse.toString();

Return the following:

Response{protocol=http/1.1,code=200,message=OK,url=https://www.google.at}

Is there any way to take statuscode as an integer, or do you need a regular expression to filter out the toString () - Method?

Solution

You can use the httpresponse class and use it to access the status code as follows;

HttpResponse httpResponse = client.newCall(request).execute(); 
httpResponse.getStatusLine().getStatusCode();

If you are using COM squareup. okhttp. Response, you can use the code () method to get the HTTP status code

Response httpResponse = client.newCall(request).execute(); 
httpResponse.code();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>