@@ -42,11 +42,14 @@ public class APIClient {
4242 private SocketConfig socketConfig ;
4343 /** debug mode flag */
4444 private boolean debugMode ;
45+ /** user agent string */
46+ private String ua ;
4547
4648 /**
4749 * Class constructor. Creates an API Client ready to use.
4850 */
4951 public APIClient () {
52+ this .ua = "" ;
5053 this .debugMode = false ;
5154 this .setURL ("https://coreapi.1api.net/api/call.cgi" );
5255 this .socketConfig = new SocketConfig ();
@@ -129,6 +132,38 @@ public String getURL() {
129132 return this .socketURL ;
130133 }
131134
135+ /**
136+ * Set a custom user agent header (useful for tools that use our SDK)
137+ *
138+ * @param str user agent label
139+ * @param rv user agent revision
140+ */
141+ public void setUserAgent (String str , String rv ) {
142+ String jv = System .getProperty ("java.vm.name" ).toLowerCase ().replaceAll (" .+" , "" );
143+ String jrv = System .getProperty ("java.version" );
144+ String arch = System .getProperty ("os.arch" );
145+ String os = System .getProperty ("os.name" );
146+ this .ua = (str + " (" + os + "; " + arch + "; rv:" + rv + ") java-sdk/" + this .getVersion ()
147+ + " " + jv + "/" + jrv );
148+ }
149+
150+ /**
151+ * Get the user agent string
152+ *
153+ * @return user agent string
154+ */
155+ public String getUserAgent () {
156+ if (this .ua .length () == 0 ) {
157+ String jv = System .getProperty ("java.vm.name" ).toLowerCase ().replaceAll (" .+" , "" );
158+ String jrv = System .getProperty ("java.version" );
159+ String arch = System .getProperty ("os.arch" );
160+ String os = System .getProperty ("os.name" );
161+ this .ua = ("JAVA-SDK (" + os + "; " + arch + "; rv:" + this .getVersion () + ") " + jv
162+ + "/" + jrv );
163+ }
164+ return this .ua ;
165+ }
166+
132167 /**
133168 * Get the current module version
134169 *
@@ -334,7 +369,7 @@ public Response request(Map<String, String> cmd) {
334369 con .setRequestMethod ("POST" );
335370 con .setRequestProperty ("Content-length" , String .valueOf (data .length ()));
336371 con .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
337- con .setRequestProperty ("User-Agent" , "JAVA-SDK::" + this .getVersion ());
372+ con .setRequestProperty ("User-Agent" , this .getUserAgent ());
338373 con .setRequestProperty ("Expect" , "" );
339374 con .setConnectTimeout (APIClient .socketTimeout );
340375 con .setReadTimeout (APIClient .socketTimeout );
0 commit comments