[英]Why am I getting 'The remote server returned an error: (401) Unauthorized.' from this code snippet?
So I copy-pasted the brief code snippet所以我复制粘贴了简短的代码片段
public ActionResult Run()
{
string apiKey = "ABCD-1234";
string value = "http://www.google.com"; // a url starting with http or an HTML string
using (var client = new WebClient())
{
// Build the conversion options
NameValueCollection options = new NameValueCollection();
options.Add("apikey", apiKey);
options.Add("value", value);
// Call the API convert to a PDF
MemoryStream ms = new MemoryStream(client.UploadValues("http://api.html2pdfrocket.com/pdf", options));
// Make the file a downloadable attachment - comment this out to show it directly inside
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=myfilename.pdf");
// Return the file as a PDF
return new FileStreamResult(ms, "application/pdf");
}
}
from http://www.html2pdfrocket.com/convert-csharp-to-pdf into my web application and am getting a 401 error with a stack trace like从http://www.html2pdfrocket.com/convert-csharp-to-pdf进入我的 Web 应用程序,并收到 401 错误,堆栈跟踪如下
[WebException: The remote server returned an error: (401) Unauthorized.] System.Net.WebClient.UploadValues(Uri address, String method, NameValueCollection data) +490
[WebException:远程服务器返回错误:(401) 未经授权。] System.Net.WebClient.UploadValues(Uri address, String method, NameValueCollection data) +490
System.Net.WebClient.UploadValues(String address, NameValueCollection data) +35 Survey.Controllers.AnswersController.Run() +163System.Net.WebClient.UploadValues(String address, NameValueCollection data) +35 Survey.Controllers.AnswersController.Run() +163
lambda_method(Closure , ControllerBase , Object[] ) +62lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数) +14
I tried adding in client = UseDefaultCredentials = true;
我尝试添加
client = UseDefaultCredentials = true;
, to no avail. ,无济于事。
Any other ideas I can try?我可以尝试其他任何想法吗?
read this documentation阅读本文档
it's saying in API Authentication Section In case you don't have an account yet you can sign up for our 14-day free trial.
它在 API 身份验证部分说 如果您还没有帐户,您可以注册我们的 14 天免费试用。 To authenticate against our web service you're required to use your API key and username.
要对我们的 Web 服务进行身份验证,您需要使用您的 API 密钥和用户名。 These can be found on your profile page and dashboard.
这些可以在您的个人资料页面和仪表板上找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.