我创建一个到身份验证服务器(OAuth 2.0)的连接来获取令牌。这样做的方法如下:
public static async Task<String> GetToken(string username, string password)
{
String response = null;
await Task.Run(() =>
{
URL url = new URL(Configuration.Configuration.baseURL + Configuration.Configuration.tokenPath);
HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection();
urlConnection.DoOutput = true;
urlConnection.DoInput = true;
urlConnection.RequestMethod = "POST";
urlConnection.SetRequestProperty("Content-Type", "application/x-www-form-urlencoded");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("client_id", "widas_servicebar"),
new KeyValuePair<string, string>("scope", Configuration.Configuration.scopes)
});
//var content = new Dictionary<string, string>();
//content.Add("grant_type", "password");
//content.Add("username", username);
//content.Add("password", password);
//content.Add("client_id", "widas_servicebar");
//content.Add("scope", Configuration.Configuration.scopes);
using (var streamWriter = new StreamWriter(urlConnection.OutputStream))
{
streamWriter.Write(content);
streamWriter.Flush();
streamWriter.Close();
}
urlConnection.Connect();
var stream = urlConnection.InputStream;
using (var streamReader = new StreamReader(stream))
{
response = streamReader.ReadToEnd();
return response;
}
});
return response;
}
不幸的是,当我调试代码时,它总是被困在var stream = urlConnection.InputStream;
,从那以后什么都没有发生(我尝试等待15分钟,使用Postman,请求在200-500ms内得到响应)。
我在其他一些方法中使用了几乎相同的代码,这些方法也建立了与服务器(内容服务器)的连接,并且工作正常。
我的代码中有错误吗?或者输入流永远不会结束的原因是什么?
如果有什么重要的话:我正在构建一个Xamarin.Android项目。
以防万一:有一个名为Xamarin.Auth的Xamarin组件提供OAuth身份验证。你不必手动实现:
Xamnarin.Forms教程:https://developer.xamarin.com/guides/xamarin-forms/web-services/authentication/oauth/
NuGet:https://www.nuget.org/packages/Xamarin.Auth/
我开始工作了。我尝试使用的东西似乎并没有像我想象的那样奏效。现在工作的代码如下:
public static async Task<String> GetToken(string username, string password)
{
String response = null;
await Task.Run(() =>
{
URL url = new URL(Configuration.Configuration.baseURL + Configuration.Configuration.tokenPath);
HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection();
urlConnection.DoOutput = true;
urlConnection.DoInput = true;
urlConnection.RequestMethod = "POST";
urlConnection.SetRequestProperty("Content-Type", "application/x-www-form-urlencoded");
var content = new Dictionary<string, string>();
content.Add("grant_type", "password");
content.Add("username", username);
content.Add("password", password);
content.Add("client_id", "widas_servicebar");
content.Add("scope", Configuration.Configuration.scopes);
Stream os = urlConnection.OutputStream;
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.Write(getQuery(content));
writer.Flush();
writer.Close();
os.Close();
urlConnection.Connect();
//var debug = urlConnection.ErrorStream;
var stream = urlConnection.InputStream;
using (var streamReader = new StreamReader(stream))
{
response = streamReader.ReadToEnd();
}
return response;
});
return response;
}
getQuery方法如下所示:
private static String getQuery(Dictionary<string, string> dictionary)
{
StringBuilder result = new StringBuilder();
bool first = true;
foreach (KeyValuePair<string, string> entry in dictionary)
{
if (first)
{
first = false;
}
result.Append(URLEncoder.Encode(entry.Key, "UTF-8"));
result.Append("=");
result.Append(URLEncoder.Encode(entry.Value, "UTF-8"));
result.Append("&");
}
return result.ToString();
}