2012-07-12 21 views
0

我真誠地懇求您的耐心和理解。我該如何將其遷移到Web服務

下面的代碼通過提供一個框和一個按鈕來工作。

該框包含一個URL和按鈕簡單地說,轉換。

如果您點擊轉換按鈕,它會打開url的內容並將其轉換爲pdf。

This works great。

不幸的是,他們希望將其翻譯爲Web服務,以便其他應用程序可以通過提供2個輸入參數,url和文檔名稱來執行類似的任務。

我已經看過創建和使用Web服務的幾個例子,雖然它們看起來相當簡單,但是編寫代碼的方式使得翻譯極其困難。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using EO.Pdf; 

public partial class HtmlToPdf : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void btnConvert_Click(object sender, EventArgs e) 
    { 
     //Create a PdfDocument object and convert 
     //the Url into the PdfDocument object 
     PdfDocument doc = new PdfDocument(); 
     HtmlToPdf.ConvertUrl(txtUrl.Text, doc); 

     //Setup HttpResponse headers 
     HttpResponse response = HttpContext.Current.Response; 
     response.Clear(); 
     response.ClearHeaders(); 
     response.ContentType = "application/pdf"; 

     //Send the PdfDocument to the client 
     doc.Save(response.OutputStream); 

     Response.End(); 
    } 
} 

如果您可以提供足夠的幫助,我真的很感激。

+1

http://whathaveyoutried.com? – 2012-07-12 21:02:33

回答

1

Web服務的最基本的形式是使用HTTP handler

public class HtmlToPdfHandler : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     string url = context.Request["url"]; 
     string documentName = context.Request["name"]; 

     PdfDocument doc = new PdfDocument(); 
     HtmlToPdf.ConvertUrl(url, doc); 
     context.Response.ContentType = "application/pdf"; 
     doc.Save(context.Response.OutputStream); 
    } 

    public bool IsReusable 
    { 
     get { return false; } 
    } 
} 

然後:http://example.com/HtmlToPdfHandler.ashx?url=someurl&name=some_doc_name

對於更高級的服務,您可以看看WCF或即將推出的Web API

+0

謝謝,謝謝,謝謝! 願上帝保佑你;這讓我一整天都很難過! 就這樣,我不會再在這種大力援助之後再犯你,我還需要用2個盒子和按鈕創建一個標記頁嗎? 最後,如果我需要調用這個,使用,例如,經典的ASP,這仍然是可能的權利? 我到目前爲止所讀的一切都指出這是可能的。 再次,非常感謝! – Kenny 2012-07-12 22:49:07

+0

任何人都可以請告訴我爲什麼我在運行Darin上面的代碼時遇到這麼多困難? 我得到這個:未能轉換網址的「http://www.google.com,/web/HtmlToPdfHandler.ashx」 當我鍵入此瀏覽器: http://test.com/test/HtmlToPdfHandler .ashx?url = someurl&name = some_doc_name,它指向這一行: HtmlToPdf.ConvertUrl(url,doc); 也許,我正確傳遞參數? – Kenny 2012-07-13 16:11:21

相關問題