2016-03-22 29 views
0

だから私は自分のWebアプリケーションにhttp://www.html2pdfrocket.com/convert-csharp-to-pdfから簡単なコードスニペットなぜ私は 'リモートサーバがエラーを返しました:(401)Unauthorized。'このコードスニペットから?

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"); 
    } 
} 

を-貼り付けコピーして、私はnoに、client = UseDefaultCredentials = true;に追加しようとした

[WebException: The remote server returned an error: (401) Unauthorized.] 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 controller, Object[] parameters) +14

のようなスタックトレースと401エラーを取得しています利用可能。

私が試すことができる他のアイデアは?

+0

を読みますか? –

+1

この文書をお読みくださいAPI認証セクションでhttps://html2pdfwebservice.com/documentationと言っています。まだアカウントをお持ちでない場合は、14日間無料トライアルにサインアップできます。 Googleのウェブサービスを認証するには、APIキーとユーザー名を使用する必要があります。これらはあなたのプロフィールページとダッシュボードにあります。 – rashfmnb

+0

ああ、お金がかかりますか?気にしないで。 –

答えて

3

は、プロキシを定義しようとしたことがあり、この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. To authenticate against our web service you're required to use your API key and username. These can be found on your profile page and dashboard.

+0

html2pdfwebservice.comはhtml2pdfrocketと同じではありません。私はHTML2PDF Webサービスを所有しているので知っています:-)。 – Htbaa

関連する問題