0

All:

There is an Bible ASP.NET Web Application that I am implementing. Within the Bible ASP.NET Web Application, I invoke the following Third-party web service API url:

https://bibles.org/v2/passages.js?q[]=john+3:1-5&version=eng-KJVA

Within in the ASP.NET Web Application, I have the following code:

String populatedPassagesEndpointUri = "https://bibles.org/v2/passages.js?q[]=john+3:1-5&version=eng-KJVA";

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(populatedPassagesEndpointUri);

request.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(ConfigurationManager.AppSettings.Get("APIToken") + ":" + "X"));

request.Method = "GET";


String test = String.Empty;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

{

  Stream dataStream = response.GetResponseStream();

  StreamReader reader = new StreamReader(dataStream);

  test = reader.ReadToEnd();

  reader.Close();

  dataStream.Close();

}

If you analyze the Web Service API url that I use above, you will notice that it uses [] square brackets which I believe is a bad practice to use within URLs:

https://bibles.org/v2/passages.js?q[]=john+3:1-5&version=eng-KJVA

Sadly, it's the third-party who provides the Web Services API, so I can't do anything about it. I'm forced to use the URL with []

In any case, I executed the code above in my Bible ASP.NET Web application. However, the above Webservice api URL invocation gives me a 401 error. Does that have something to do with the [] square brackets in the URL? If yes, is there a way of escaping the [] square brackets in the URL before invoking the Web SErvice API URL call?

crazyTech
  • 1,379
  • 3
  • 32
  • 67

3 Answers3

2

You should use this

HttpUtility.UrlEncode()

You code will become

String populatedPassagesEndpointUri = HttpUtility.UrlEncode("https://bibles.org/v2/passages.js?q[]=john+3:1-5&version=eng-KJVA");

to encode the url of your request.

Similar question on SO

  1. Url with square brackets in webClient.DownloadString
  2. Are square brackets permitted in URLs?
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
1

I guess the problem is not the URL, is the authentication method you are using.

the above Webservice api URL invocation gives me a 401 error

401 error means you failed on authentication.

I tried the URL you provided. It does accept Basic authentication, but I doubt the format you provide.

From your code, you are saying that your basic authentication username is Some API token , and your password is X or something. Seems not right.

The realm your server return is ABS API , and the error message I got is

Authorization Required

You must provide an API token or a username and password to access this page.

So I guess you should use token based authentication since you got an APIToken from your config file , not a useranme and password.

The way to do token authentication is various.

It could be something like

https://bibles.org/v2/passages.js?q[]=john+3:1-5&version=eng-KJVA&token=your API token

Or passing the token in authentication header like

Bearer yourAPItoken

Overall, you need to double check your authentication method,try to find some document about your website or ask for support form the website.

Update:

The reason of you getting 401 error is because your original request get redirected, and your authentication header will not survive redirection.

You need set your httpWebRequest.AllowAutoRedirect = false , and test the respond code, if = 302 found, create a new request to the redirect location.

This may help you http://www.codeproject.com/Articles/49243/Handling-Cookies-with-Redirects-and-HttpWebRequest

Community
  • 1
  • 1
LeY
  • 659
  • 7
  • 21
  • I just briefly browsed your website. Try remove your authentication header and change your URL like this format 'https://#{your API token}:bibles.org/v2/passages.js?q[]=john+3:1-5&version=eng-KJVA' – LeY Oct 16 '14 at 06:20
  • I sent an email to their support email address ( digitalministry@americanbible.org ). Let's see what they say. – crazyTech Oct 16 '14 at 06:54
  • `https://#{your API token}:bibles.org/v2/passages.js?q[]=john+3:1-5&version=eng-KJVA` doesn't work for you? – LeY Oct 16 '14 at 06:55
  • try this`request.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("#{" + ConfigurationManager.AppSettings.Get("APIToken") + "}" + ":" + "X"));` – LeY Oct 16 '14 at 07:02
  • If you look at my code, I've already done all that. Here is what I have request.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(ConfigurationManager.AppSettings.Get("APIToken") + ":" + "X")); No need for "#{" and "}" because other Web Service API urls provided by the third-party provider work with the Basic authorization code that I placed in this comment. – crazyTech Oct 16 '14 at 07:05
1

Here is my own implementation, you can modify it to suite your needs:

Firstly, inject IHttpContextAccessor into the class where you are trying to implement this code;

Then utilize my code below, to suite what you want to achieve:

        var request = _httpConAccessor.HttpContext.Request;

        var builder = new UriBuilder();
        builder.Scheme = request.Scheme;
        builder.Port = request.Host.Port.GetValueOrDefault();


        builder.Path = "/Home/ConfirmEmail";
        var query = HttpUtility.ParseQueryString(builder.Query);
        query["userId"] = user.EmailAddress;
        query["token"] = user.ActivetionCode.ToString();
        builder.Query = query.ToString();
        string link = builder.ToString();

        var link2 = HttpUtility.UrlDecode(link);

        return link2;

The output to this function looks like this for me:

https://localhost:44330/Home/ConfirmEmail?userId=myemailaddress@gmail.com&token=b5867906-7ede-44yd-adrt4-0272a5cacd

instead of

https://[localhost:44330]/Home/ConfirmEmail?userId=myemailaddress@gmail.com&token=b5867906-7ede-44yd-adrt4-0272a5cacd

Observe that the square brackets are absent.

Happy Coding.

Chidi-Nwaneto
  • 634
  • 7
  • 11