简体   繁体   中英

Unable to connect to the remote server with HTMLworker

I m developing a web application in C# and using itextsharp.text elements. I m very much new to itextsharp and its classes. Please Help me with this error: Unable to connect to the remote server

Here is my code and stack trace:

public void CreatePDFDocument(string strHtml) 
{
        DirectoryInfo thisFolder = new DirectoryInfo(Server.MapPath("Temp"));
        if (thisFolder.Exists)
        {
            if (File.Exists(Server.MapPath("Temp/test.pdf")))
            {
                File.Delete(Server.MapPath("Temp/test.pdf"));
            }

            thisFolder.Delete();
            thisFolder.Create();
        }
        else
        {
            thisFolder.Create();

        }
        string strFileName =Server.MapPath("Temp/test.pdf");
        // step 1: creation of a document-object
        Document document = new Document();
        // step 2:
        // we create a writer that listens to the document
        PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
        StringReader se = new StringReader(strHtml);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
        obj.Parse(se); // this line throws error
        document.Close();
        ShowPdf(strFileName);
}

The line in BOLD throws error.

Here is the stack trace:

System.Net.WebException occurred

HResult=-2146233079

Message=Unable to connect to the remote server

Source=System

StackTrace: at System.Net.HttpWebRequest.GetResponse()

at iTextSharp.text.Image.GetInstance(Uri url, Boolean handleIncorrectImage)

at iTextSharp.text.Image.GetInstance(Uri url)

at iTextSharp.text.Image.GetInstance(String filename)

at iTextSharp.text.html.simpleparser.ElementFactory.CreateImage(String src, IDictionary 2 attrs, ChainedProperties chain, IDocListener document, IImageProvider img_provider, Dictionary 2 img_store, String img_baseurl) at iTextSharp.text.html.simpleparser.HTMLWorker.CreateImage(IDictionary`2 attrs)

at iTextSharp.text.html.simpleparser.HTMLTagProcessors.HTMLTagProcessor_IMG.StartElement(HTMLWorker worker, String tag, IDictionary`2 attrs)

at iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, IDictionary`2 attrs)

at iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start)

at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader)

at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html)

at iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader)

at Payroll.PaySlip.CreatePDFDocument(String strHtml) in F:\\New folder\\EMS\\EMS\\Payroll\\PaySlip.aspx.cs:line 88

InnerException: System.Net.Sockets.SocketException

HResult=-2147467259

Message=No connection could be made because the target machine actively refused it 127.0.0.1:5510

Source=System

ErrorCode=10061

NativeErrorCode=10061

StackTrace: at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)

at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)

InnerException:

Unsure without content of HTML, but is this applicable to you: http://forums.asp.net/t/1841173.aspx ?

quote from that page:

As your html string contain Images that are pointing to some external server. and iTextSharp library is trying to get images from those server and failed to do so, that's why you are getting those error.

Try to acces thorse images from your browser in same machine.

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