简体   繁体   中英

How can I migrate this to Web Service

I sincerely beg for your patience and understanding.

The below code works by providing a box and a button.

The box contains a url and the button simply says, Convert.

If you click the convert button, it opens the contents of the url and converts it to pdf.

This works great.

Unfortunately, they want it translated as web service so other apps can use it to perform similar task by providing 2 input params, url and document name.

I have looked at several examples of creating and consuming web services, while they seem fairly simple, the way this code is written makes it extemely difficult to translate.

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

If you could be kind enough to assist, I am truly grateful.

The most basic form of a web service is to use an 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; }
    }
}

and then: http://example.com/HtmlToPdfHandler.ashx?url=someurl&name=some_doc_name

For more advanced services you might take a look at WCF or the upcoming Web API .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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