Я использую следующий код для вызова почтового интерфейса в Visual Studio 2022:
private List LoginToken(string username, string password)
{
string api = "";
List list = new List();
string url = "";
Dictionary dics = new Dictionary
{
{ "username", username },
{ "password", password },
{ "loginType", "account" },
{ "grantType", "password" }
};
Task task = Api.InvokeWebapi(url, api, "POST", dics);
string result = task.Result;
if (result != null)
{
JObject jsonObj = null;
jsonObj = JObject.Parse(result);
DataInfo info = new DataInfo();
info.statusCode = Convert.ToInt32(jsonObj["code"]);
info.message = jsonObj["message"].ToString();
if (info.statusCode == 1)
{
JArray jlist = JArray.Parse(jsonObj["data"].ToString());
for (int i = 0; i < jlist.Count; ++i)
{
Token ver = new Token();
JObject tempo = JObject.Parse(jlist.ToString());
ver.access_token = tempo["access_token"].ToString();
ver.token_type = tempo["token_type"].ToString();
ver.expires_in = Convert.ToInt32(tempo["expires_in"]);
ver.scope = tempo["scope"].ToString();
ver.name = tempo["name"].ToString();
ver.my_Corp = tempo["my_Corp-Code"].ToString();
ver.userId = Convert.ToInt32(tempo["userId"]);
ver.username = tempo["username"].ToString();
ver.jti = tempo["jti"].ToString();
list.Add(ver);
}
}
}
return list;
}
public async Task InvokeWebapi(string url, string api, string type, Dictionary dics)
{
string result = string.Empty;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Basic 1111");
client.BaseAddress = new Uri(url);
client.Timeout = TimeSpan.FromSeconds(510);
if (type.ToLower() == "put")
{
HttpResponseMessage response;
if (dics.Keys.Contains("input"))
{
if (dics != null)
{
foreach (var item in dics.Keys)
{
api = api.Replace(item, dics[item]).Replace("{", "").Replace("}", "");
}
}
var contents = new StringContent(dics["input"], Encoding.UTF8, "application/json");
response = client.PutAsync(api, contents).Result;
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
return result;
}
return result;
}
var content = new FormUrlEncodedContent(dics);
response = client.PutAsync(api, content).Result;
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
return result;
}
}
else if (type.ToLower() == "post")
{
var content = new FormUrlEncodedContent(dics);
HttpResponseMessage response = client.PostAsync(api, content).Result;
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
return result;
}
}
else if (type.ToLower() == "get")
{
HttpResponseMessage response = client.GetAsync(api).Result;
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
return result;
}
}
else
{
return result;
}
return result;
}
}
Я использую приведенный выше код для вызова почтового интерфейса в Visual Studio 2022, но в jsonObj = JObject.Parse(result); отображается ошибка Newtonsoft.Json. JsonReaderException: «Ошибка чтения JObject из JsonReader. Путь '', строка 0, позиция 0».
Обновление:
Я захватил ответ API при сообщении об ошибке:
{StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Pragma: no-cache
X-Frame-Options: DENY
Referrer-Policy: no-referrer
Cache-Control: no-store, must-revalidate, no-cache, max-age=0
Date: Wed, 17 Jul 2024 08:47:38 GMT
Server: nginx/1.24.0
Content-Type: application/json
Expires: 0
}}
Подробнее здесь: https://stackoverflow.com/questions/787 ... position-0
Ошибка чтения JObject из JsonReader. Путь '', строка 0, позиция 0». ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение