locked
Illegal characters in path. RRS feed

  • Question

  • I want to consume a service at http://www.esvapi.org/api/#sample. The sample code in the link is

    C#
    REST
    
    StringBuilder sUrl = new StringBuilder();
    sUrl.Append("http://www.esvapi.org/v2/rest/passageQuery");
    sUrl.Append("?key=IP");
    sUrl.Append("&passage=" + Server.UrlEncode("Matthew 5").ToString());
    
    sUrl.Append("&include-headings=true");
    
    WebRequest oReq = WebRequest.Create(sUrl.ToString());
    StreamReader sStream = new StreamReader(oReq.GetResponse().GetResponseStream());
    
    StringBuilder sOut = new StringBuilder();
    sOut.Append(sStream.ReadToEnd());
    sStream.Close();
    
    Response.Write(sOut.ToString());

    I rewrote it with my own.

    var strSearch = "John 3:16";
        var options = "include-passage-references=true";
        var client = new WebClient();
        var query = string.Format("www.esvapi.org/v2/rest/passageQuery?key={0}&passage={1}&options={2}", "IP", Server.UrlEncode(strSearch), options);
        var result = client.DownloadString(query);

    However I got an exception which is "System.ArgumentException: Illegal characters in path."

    The variable query is

    www.esvapi.org/v2/rest/passageQuery?key=IP&passage=John+3%3a16&options=include-passage-references=true

    What is wrong?

    Friday, June 28, 2013 6:25 PM

Answers

  • The URL you are using isn't a full URL so it is probably bombing on that.  Try appending http://.  Alternatively consider using the BaseAddress and QueryString properties to set up the address rather than building it by hand.  Alternatively use the UriBuilder class to build the URL and then pass the generated Uri as the parameter.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by ardmore Friday, June 28, 2013 6:36 PM
    Friday, June 28, 2013 6:30 PM