How might I use the W3C Markup Validator API in my .NET application?

Deadly 提交于 2019-12-23 03:58:10

问题


I found that there's an API for the W3C Markup Validator.

I had earlier asked: Is there a .NET library for the W3C Markup Validator API?

Assaf's answer:

This API is SOAP based. If you want to use it in a .net application you can just add the web reference and code against it. Seems simple enough as it's basically a one-method API...

So, I tried to "Add Service Reference" at address http://validator.w3.org/check.

First the dialog displays:

Please wait for service information to be downloaded...

Then:

An error ... occurred while attempting to find services at 'http://validator.w3.org/check'

Visual Studio Add Service Reference Dialog http://img17.imageshack.us/img17/719/addservicereference.gif

Error details:

The HTML document does not contain Web service discovery information. Metadata contains a reference that cannot be resolved: 'http://validator.w3.org/check'. The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">   <head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>

        Validation Results - W3C Markup Validator</title>
    <link rel="icon" href="data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%02%00%00%00%90%91h6%00%00%00%19IDAT(%91c%BCd%AB%C2%40%0A%60%22I%F5%A8%86Q%0DCJ%03%00%DE%B5%01S%07%88%8FG%00%00%00%00IEND%AEB%60%82" type="image/png" />
<link rev="made" href="mailto:www-validator@w3.org" />
<link rev="start" href="./" title="Home Page" />
<style type="text/css" media="all">@import "./style/base.css";
        @import "./style/results.css";</style>
<meta name="keywords" content="HTML, HyperText Markup Language, Validation,
  W3C Markup Validation Service" />
<meta name="description" content="W3C's easy-to-use
  H

If the service is defined in the current solution, try building the solution and adding the service reference again.

How can I use the W3C Markup Validator API in my .NET application?


回答1:


The W3C Service is no standard SOAP Service! It can give a SOAP formatted response but to call it's a sinple REST URL based Service http://validator.w3.org/check?uri=YourURLToProof&charset=utf-8&output=soap12




回答2:


In order for a Web Reference to work, I think it needs the owner of the service to have published a WSDL file for .Net to read and create local objects with. You then call these local objects in your project, and they get populated with Data from the other end of the service using SOAP.

I've been looking for a WSDL file that describes the W3C's validation SOAP on their site somewhere, but no luck so far. Which is odd given the W3C manage the WSDL protocol. You'd really expect them to use it!

If anyone knows:

  1. A way to get a Web Reference working without WSDL or...
  2. Where the W3C Validator's WSDL file is...

Then please let me know...

Failing that - the W3C have put a link to a C# library on their site (http://validator.w3.org/docs/api.html#libs), which is easy enough to download and build. But that uses LINQ to build an object based on the SOAP returned by the W3C - which seems a bit heavy for my purposes... It's a useful starting point if nothing else tho.




回答3:


A SOAP Web Service must have a WSDL. I have seen some mentions on the W3C site of a SOAP API, but the location of the API or the WSDL to it is not apparent.

Poking around and searching with Bing, I found the following: http://www.w3.org/Search/Mail/Public/search?type-index=www-validator&index-type=t&keywords=wsdl&search=Search

Good luck. It seems they've done this a bit backwards, with the web service being an afterthought from people who don't quite get the concept of a WSDL.




回答4:


You can actually download and install the validator on to your own site from https://github.com/validator/validator

Some useful notes from the about page

I made a quick c# validator method from calling: https://validator.w3.org/nu/?doc=http://www.example.com&out=json

like so:

using Newtonsoft.Json;
using System.Net;
using System.Net.Http;

private void ValidateFromW3Org(string url)
{
    HttpClientHandler clientHandler = new HttpClientHandler();
    HttpClient client = new HttpClient(clientHandler);
    client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent",
            "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");

    string validator = string.Format(
        "http://validator.w3.org/nu/?doc={0}&out=json", url);

    string response = client.GetStringAsync(url).Result;

    PageValidationResult pageResults = JsonConvert.DeserializeObject<
        PageValidationResult>(response);
    IList<ValidationResult> results = pageResults.Messages;

    foreach(ValidationResult result in results)
    {
        Console.WriteLine("{0}:{1} line: {2} - {3}", result.Type,
            result.SubType, result.LastLine, result.Message);
    }
}

public class ValidationResult
{
    public string Type { get; set; }
    public string SubType { get; set; }
    public int LastLine { get; set; }
    public int FirstColumn { get; set; }
    public int LastColumn { get; set; }
    public string Message { get; set; }
    public string Extract { get; set; }
    public int HiliteStart { get; set; }
    public int HiliteLength { get; set; }
}

public class PageValidationResult
{
    public string Url { get; set; }
    public IList<ValidationResult> Messages { get; set; }
}

Please note this is a just a sample. You wouldn't want to re-use HttpClient in a method that way. This also uses third party Newtonsoft.Json to parse json result.



来源:https://stackoverflow.com/questions/615132/how-might-i-use-the-w3c-markup-validator-api-in-my-net-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!