繁体   English   中英

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

[英]Why am I getting 'The remote server returned an error: (401) Unauthorized.' from this 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");
    }
}

http://www.html2pdfrocket.com/convert-csharp-to-pdf进入我的 Web 应用程序,并收到 401 错误,堆栈跟踪如下

[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
lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数) +14

我尝试添加client = UseDefaultCredentials = true; ,无济于事。

我可以尝试其他任何想法吗?

阅读本文档

它在 API 身份验证部分说 如果您还没有帐户,您可以注册我们的 14 天免费试用。 要对我们的 Web 服务进行身份验证,您需要使用您的 API 密钥和用户名。 这些可以在您的个人资料页面和仪表板上找到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM