开发者

Web service XML over HTTP

开发者 https://www.devze.com 2023-03-25 04:00 出处:网络
I\'m trying to use a web service that doesn\'t use SOAP and WSDL but i don\'t know how to do it. I would really appreciate some pointers.

I'm trying to use a web service that doesn't use SOAP and WSDL but i don't know how to do it. I would really appreciate some pointers.

The API for the service is:

http://someaddress.com/webservices/name/id where id is the parameter.

The supported request method is GET.

Could i use something like this:

var myReq = new XMLHttpRequest();
var url = "http://someaddress.com/webservices/name/2"
开发者_高级运维myReq.open("GET", url, true);
myReq.send();


The simplest way to get an xml is to use the url as an argument to XDocument.Load() method.

var xml = XDocument.Load("http:...");

This method fetches the data from a remote url, parses it using an XmlReader an constructs an XDocument object. Then you can use LINQ to XML to query or transform data.

Unfortunately, this wouldn't work for POST, DELETE, PUT http requests

Edit: It depends on your service and what operations you can do with it:

  1. Using XDocument.Load() is the simplest solution. If this is a simple resource over the internet, with no authentication, no HTTp headers needed and supports only GET requests than this is the way to go. You can write a method which take your parameters and appends them in the URL

    public SomeClass GetSomeEntity(string id) 
    {
        var xml = XDocument.Load("http://mywebservice.com/ws/" + id);
        // transform xml into an instance of actual type
    }
    
  2. Using a WebClient you can get more control over your HttpRequest. You can set basic authentication credentials, append other HTTP headers, POST form-data etc. You have "async" methods also.

    using (var client = new WebClient()) 
    {
       var xml = XDocument.Load(client.OpenRead("http://yoururl.com");
       // process xml
    } 
    
  3. You can think of "HttpWebRequest" as a low-level implementation of a web request.


Here is a snipped of code to help you do this..

public static string SendRequest(string uri, string method, string contentType, string body)
{
    string responseBody = String.Empty;

    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(uri));
    req.Method = method;
    if (!String.IsNullOrEmpty(contentType))
    {
        req.ContentType = contentType;
    }
    if (!String.IsNullOrEmpty(body))
    {
        byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
        req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
        req.GetRequestStream().Close();
    }

    HttpWebResponse resp;
    try
    {
        resp = (HttpWebResponse)req.GetResponse();
    }
    catch (WebException e)
    {
        resp = (HttpWebResponse)e.Response;
    }

    Stream respStream = resp.GetResponseStream();
    if (respStream != null)
    {
        responseBody = new StreamReader(respStream).ReadToEnd();
    }

    return responseBody;
}


The WebClient object is very well done for those kind of tasks.


Check out the WCF REST Developer Center - it shows you how easily and efficiently you can create REST services (no SOAP) using the WCF infrastructure.


Using JQuery is the simplest as far as I know. Try if this works in your case:

var param = new Object();
    param.id = 2;

    $.ajax({
        type: "GET",
        url: "http://someaddress.com/webservices/name",
        data: $.toJSON(param),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d) {
              //do something 
            } 
        }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号