Created
January 18, 2022 19:30
-
-
Save hihanley/6792178c1726294e2fde9c51956526e1 to your computer and use it in GitHub Desktop.
Java retrofit2 请求设置代理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import lombok.Data; | |
import okhttp3.OkHttpClient; | |
import retrofit2.Call; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.jackson.JacksonConverterFactory; | |
import retrofit2.http.GET; | |
public class Demo{ | |
@Data | |
static class IPInfoJSONResponse { | |
String ip; | |
String hostname; | |
String city; | |
String region; | |
String country; | |
String loc; | |
String org; | |
String timezone; | |
String readme; | |
String postal; | |
} | |
public interface IPInfoService { | |
@GET("json") | |
Call<IPInfoJSONResponse> json(); | |
} | |
public static void main(String[] args) throws IOException { | |
var proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1080)); | |
var client = new OkHttpClient.Builder().proxy(proxy).build(); | |
var retrofit = new Retrofit.Builder() | |
.client(client) | |
.baseUrl("https://ipinfo.io") | |
.addConverterFactory(JacksonConverterFactory.create()) | |
.build(); | |
var service = retrofit.create(IPInfoService.class); | |
var request = service.json(); | |
var result = request.execute().body(); | |
System.out.println(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment