我创建了与身份验证服务器(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项目。
我知道了。似乎我尝试使用的东西没有按我认为的那样起作用。现在工作的代码如下所示:
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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句