我想在一次api调用中使用改进来改变OkHttp客户端上的读取超时。要明确的是,我有一个可能需要很长时间的终点,我需要增加它的超时时间,并且只需要一次api调用的超时。有没有办法通过注释来做到这一点?有没有办法在不更改应用程序其余部分的超时的情况下执行此操作?
答案 0 :(得分:2)
我面临着类似的情况。我解决了我在using Spring.Social.Dropbox.Api;
using Spring.Social.Dropbox.Connect;
using Spring.Social.OAuth1;
private void ShareViaDropbox(string[,] filesNameLink)
{
try
{
if (CheckForInternetConnection()) // cant use dropbox without internet.
{
string consumerKey = "<get this from dropbox>";
string consumerSecret = "<get this from dropbox>";
DropboxServiceProvider dropboxServiceProvider = new DropboxServiceProvider(consumerKey, consumerSecret, AccessLevel.Full);
IOAuth1Operations oauthOperations = dropboxServiceProvider.OAuthOperations;
// I used an xml file to store the key (probably not the most secure later I will write an encryption).
string[] storedToken = GetTag("<DropBoxToken>", ",").Split(',');
OAuthToken oauthAccessToken = new OAuthToken(storedToken[0], storedToken[1]);
// testing if we got something from the earlier xml tag.
if (oauthAccessToken.Value == string.Empty || oauthAccessToken.Value == "")
{
Console.Write("Getting request token...");
OAuthToken oauthToken = dropboxServiceProvider.OAuthOperations.FetchRequestTokenAsync(null, null).Result;
Console.WriteLine("Done");
OAuth1Parameters parameters = new OAuth1Parameters();
string authenticateUrl = dropboxServiceProvider.OAuthOperations.BuildAuthorizeUrl(oauthToken.Value, parameters);
Console.WriteLine("Redirect user for authorization");
// let the user know they have to sign in and chose the correct dropbox. this is also where most documentation leaves off!
MessageBox.Show(
"This is the first time you have connected to DropBox. A browser will open and direct you the the DropBox authentication page. When you prompted, please sign in and then choose the [named] DropBox." + Environment.NewLine +
"Once you have succesfully compleaded the authorization confirmation from DropBox, you may close the browser to continue." + Environment.NewLine +
Environment.NewLine +
"Click 'Ok' to proceed", "DropBox Authentication Required.", MessageBoxButtons.OK, MessageBoxIcon.Information);
Process.Start(authenticateUrl);
// halt the program until the user has authenticated.
MessageBox.Show("Click 'OK' when authorization has succeeded.", "DropBox Authentication - WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Console.Write("Press any key when authorization attempt has succeeded");
Console.Write("Getting access token...");
AuthorizedRequestToken requestToken = new AuthorizedRequestToken(oauthToken, null);
oauthAccessToken = dropboxServiceProvider.OAuthOperations.ExchangeForAccessTokenAsync(requestToken, null).Result;
Console.WriteLine("Done");
ChangeTag("<DropBoxToken>", oauthAccessToken.Value + "," + oauthAccessToken.Secret); // update the xml file with the token.
}
IDropbox dropbox = dropboxServiceProvider.GetApi(oauthAccessToken.Value, oauthAccessToken.Secret);
DropboxLink shareableLink;
double filesNameLength = Math.Ceiling(Convert.ToDouble(filesNameLink.Length / 2));
// remove everything before the users special folder path including the dropbbox folder.
// then change all the switches to front from back for the web.
for (int i = 0; i < filesNameLength; i++)
{
filesNameLink[i, 1] = filesNameLink[i, 0];
filesNameLink[i, 0] = Path.GetFileName(filesNameLink[i, 0]);
filesNameLink[i, 1] = filesNameLink[i, 1].Replace(dropboxRootDir, "").Replace("\\", "/");
shareableLink = dropbox.GetShareableLinkAsync(filesNameLink[i, 1].Replace("\\", "/")).Result;
filesNameLink[i, 1] = shareableLink.Url;
}
// do something with the link(s). //
catch { }
}
}
catch { }
}
中提供两个Api实例的问题,每个实例都有自己的ApiModule
。使用OkHttpClient
标识每一个。
我试图避免仅为超时配置提供两个实例,对我来说似乎有点奇怪,但由于我的API实例是单例(性能),我看不到其他解决方案。
答案 1 :(得分:0)
您实际上可以进行每次通话配置
复制默认构建器并创建另一个客户端
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://blah_blah_api.com/") // This URL is served with a 1 second delay.
.build();
try {
// Copy to customize OkHttp for this request.
OkHttpClient copy = client.newBuilder()
.readTimeout(500, TimeUnit.MILLISECONDS)
.build();
Response response = copy.newCall(request).execute();
System.out.println("Response 1 succeeded: " + response);
} catch (IOException e) {
System.out.println("Response 1 failed: " + e);
}
try {
// Copy to customize OkHttp for this request.
OkHttpClient copy = client.newBuilder()
.readTimeout(3000, TimeUnit.MILLISECONDS)
.build();
Response response = copy.newCall(request).execute();
System.out.println("Response 2 succeeded: " + response);
} catch (IOException e) {
System.out.println("Response 2 failed: " + e);
}
}