简体   繁体   English

为什么我收到“远程服务器返回错误:(401) 未经授权”。 从这个代码片段?

[英]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 likehttp://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() +163 System.Net.WebClient.UploadValues(String address, NameValueCollection data) +35 Survey.Controllers.AnswersController.Run() +163
lambda_method(Closure , ControllerBase , Object[] ) +62 lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.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.

相关问题 远程服务器返回错误:(401) Unauthorized。 Twitter oAuth - The remote server returned an error: (401) Unauthorized. Twitter oAuth 远程服务器返回错误:(401)未经授权。 wcf httpbinding基本 - The remote server returned an error: (401) Unauthorized. wcf httpbinding basic 出现错误:远程服务器返回错误:(401)未经授权。 ,同时将我的网站评论发布到Twitter。 使用C# - Getting error : The remote server returned an error: (401) Unauthorized. , while posting my website comments to twitter. Using C# C#JIRA工作日志更新错误“远程服务器返回错误:(401)未经授权。” - C# JIRA work-log update error “The remote server returned an error: (401) Unauthorized.” System.Net.WebException:远程服务器返回错误:(401)未经授权。 在System.Net.HttpWebRequest.GetResponse() - System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() SharePoint 仅应用程序身份验证抛出“远程服务器返回错误:(401)未经授权。” - SharePoint App-Only Authentication throwing “The remote server returned an error: (401) Unauthorized.” 远程服务器返回错误:(401) 未经授权。 在 Visa 开发支付网关 - The remote server returned an error: (401) Unauthorized. In Visa develope payment Gateway EWS:“远程服务器返回错误(401)未经授权” - EWS: “The remote server returned error (401) Unauthorized” 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM