3

I have a WCF service which I want to pass URL strings to. However, whenever it encounters a '%' or '/' character it falls over.

Example from javascript, this works

$.post("http://localhost:15286/Service1.svc/Submit/thor");

But neither of these do:

$.post("http://localhost:15286/Service1.svc/Submit/http://www.google.com");
$.post("http://localhost:15286/Service1.svc/Submit/http:%");

I have a breakpoint in my service and it doesn't even get hit for the last two examples.

I'm new to WCF services so I might just be making a rookie mistake.

WCF Service

[ServiceContract]
public interface IService1
{
    [WebInvoke(UriTemplate = "/Submit/{imageURL}")]
    [OperationContract]
    string Submit(string imageURL);
}


public class Service1 : IService1
{
    public string Submit(string imageURL)
    {
        return String.Format("Thanks, you sent me '{0}'.", imageURL);
    }
}
2
  • Can you try a test with argument th%20or and let us know the results? I would expect that to resolve to th<space>or server-side. Commented Feb 24, 2012 at 14:21
  • @RB th%20or worked. So it isn't the % that is the problem. Based on that I tried encodeURIComponent("google.com") which resolves to http%3A%2F%2Fwww.google.com which failed. It doesn't like the %3A, the %F2 or the '.' characters :( Commented Feb 24, 2012 at 14:41

2 Answers 2

4

Try encoding the last part of the URL with the encodeURIComponent JavaScript function:

var parameter = encodeURIComponent("http://www.google.com")
var url = "http://localhost:15286/Service1.svc/Submit/" + parameter 
$.post(url);

However, you might still get the same error due to WCF not correctly parsing some URL encoded characters. This seems to be a bug in WCF, to which there are a couple of known workarounds:

  • Pass the encoded URL parameter as a query string parameter
  • Configure how URI should be parsed through the <schemeSettings> configuration element available in .NET 4.0

You could start by passing the encoded URL as a query string parameter and see if it works. Here's an example:

[ServiceContract]
public interface IMyService
{
    [WebInvoke(UriTemplate = "/Submit?url={imageURL}")]
    [OperationContract]
    string Submit(string imageURL);
}

and on the client:

var parameter = encodeURIComponent("http://www.google.com")
var url = "http://localhost:15286/Service1.svc/Submit?url=" + parameter 
$.post(url);
Sign up to request clarification or add additional context in comments.

8 Comments

That will encode the whole URL. He only needs to encode the argument :)
Unfortunately that gives me the same result. I think the problem is on the web service side of things possibly?
Encoding the argument gives the same result. As encoding it will add '%' signs and the web service doesn't seem to like them either :(
@user914082 Did you try using encodeURIComponent as in my answer? That will encode the : and / characters, which I'm sure will fix your problem.
Yay, changing it to use a query string parameter did the job. Thanks everyone for your help :)
|
1

Use the encodeURIComponent function to correctly encode your parameter, e.g:

$.post("http://localhost:15286/Service1.svc/Submit/" + 
       encodeURIComponent("http://www.google.com"));

Note that the encodeURI function will not encode the / or : character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.