Response status code does not indicate success: 401 (Unauthorized).
在使用ASP.NET5调用IBM Bluemix上的REST API服务时出现此异常;
Response status code does not indicate success: 401 (Unauthorized).
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()at dotnethelloworld.Controllers.ValuesController.<Get>d__1.MoveNext()
相关代码:
// GET api/values/txt[HttpGet("{txt}")]public async Task<string> Get(string txt){String retString = txt;try{//构造url,source为要翻译文本的语言,target指定要翻译为什么语言String uri = "https://gateway.watsonplatform.net/language-translation/api/v2/translate?";uri += "source=en&target=es&text=" + txt;System.Net.Http.HttpClientHandler handler = new System.Net.Http.HttpClientHandler();NetworkCredential myCred = new NetworkCredential("用户名", "密码", "gateway.watsonplatform.net");CredentialCache myCache = new CredentialCache();myCache.Add(new Uri(uri), "Basic", myCred);handler.Credentials = myCache;System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(handler);retString = await httpClient.GetStringAsync(uri);return "{\"text\":\"" + retString + "\"}";}catch (Exception ex){return ex.Message;//throw ex;}}
MSDN中关于NetworkCredential 类的使用示例:
NetworkCredential myCred = new NetworkCredential(SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);CredentialCache myCache = new CredentialCache();myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);WebRequest wr = WebRequest.Create("www.contoso.com");
wr.Credentials = myCache;
只是它是用的WebRequest,而我是用的HttpClient,却不生效,没有找到具体原因;但可以通过另一种方法来解决用户验证的问题。
解决方法:
设置httpClient.DefaultRequestHeaders.Authorization 属性,代码如下:
System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
//将服务凭证转换为Base64编码格式
byte[] auth = Encoding.UTF8.GetBytes("用户名:密码");
String auth64 = Convert.ToBase64String(auth);
//创建并指定服务凭证,认证方案为Basic
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth64);retString = await httpClient.GetStringAsync(uri);
Response status code does not indicate success: 401 (Unauthorized).相关推荐
- status code = 404, status message = [Not Found]
Caused by: java.io.IOException: Did not receive successful HTTP response: status code = 404, status ...
- ajax判断成功状态码,Ajax返回状态码200,成功函数不触发(Ajax returns status code 200, success function not triggering)...
Ajax返回状态码200,成功函数不触发(Ajax returns status code 200, success function not triggering) 关于这一点有很多话题,但没有一个 ...
- Js 跨域CORS报错 Response for preflight has invalid HTTP status code 405
调用接口遇到Response for preflight has invalid HTTP status code 405这样的错误,是使用PUT方式提交请求接口.Content-Type设置为app ...
- cdp4j The status code of the opening handshake response is not ‘101 Switching Protocols‘
异常如下 Exception in thread "main" io.webfolder.cdp.exception.CdpException: io.webfolder.cdp. ...
- Gradle下载spring包出现Received status code 401 from server: Unauthorized
大家在下载spring家族源码的时候,在进行gradle构建的时候,会出现y: Could not GET 'https://repo.spring.io/libs-release/xxxxx'. R ...
- scrapy抓取豆瓣网信息时报错提醒INFO: Ignoring response <403 https://movie.douban.com/top250>: HTTP status code is
自学python的爬虫scrapy,可能会遇到如下问题: 通过上文解释对豆瓣网进行抓取过程中出现报错如下: 2020-08-20 14:27:46 [scrapy.extensions.telnet] ...
- xxx.jar‘. Received status code 401 from server: Unauthorized
目录 背景 解决方案 背景 gradle下载依赖时报一堆的 Received status code 401 from server: Unauthorized问题,具体异常如下所示. <ij_ ...
- http status code —— http 状态码
0. 网络连接的问题 vs 域名解析的问题 测试的方法主要有两个, 如果是域名解析出了问题,浏览器将会很快给出响应 如果将域名转换为对应的ip地址,浏览器能对其该ip进行响应的话,则证明是网络连接的问 ...
- HTTP状态码(HTTP Status Code)
HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.它由 RFC 2616 规范定义的,并得到RFC 2518.RFC 2817.RFC 2295.R ...
最新文章
- 受用一生的高效PyCharm使用技巧
- 【hiho】38 二分·二分答案【二分答案】
- Notepad++的使用
- windows安全模式_Winclone 8 for Mac(Windows分区备份迁移和还原工具)
- calendar获取本周一的日期_Java Calendar 获取本周周一和周日代码
- 使用 docker+tmux 加强容器调度
- java 809 128 题_java编程题809*??=800*??+9*??+1
- 学环境艺术设计的 考计算机,环境艺术设计专业升本要考哪些科目?
- 帮你找影视剧同款的“影觅”宣布完成百万美元级Pre-A轮融资_36氪
- “土地信息+”能否激发千亿大数据应用市场
- labelimg标注的VOC格式标签xml文件和yolo格式标签txt文件相互转换
- IEEE 754 32bit浮点标识
- Tessent scan ATPG(7) 设计规则检查 (DRC)
- KVM虚拟化技术之使用Qemu-kvm创建和管理虚拟机
- Alist常用样式代码
- JAVA设计模式什么鬼(中介)——作者:凸凹里歐
- Thinkphp实战(一)——项目配置与模板导入
- 中国式差旅管理的一匹黑马
- 江民科技创始人 王江民 传奇一生
- etcdctl 基本使用