0

I'm having a very strange issue.

My company uses a centralized user registration web-service for our various properties. We generally send a request to the web service via HttpURLConnection with request-method GET, setting parameters via qs. This has worked fine in the past.

For another property with which we've recently acquired and plugged into our registration web service, HttpURLConnection seems to be duplicating parameters when sent along. The expected value of a parameter is paramName=value, but we're receiving paramName=value, value instead. Here's a representation of what it looks like in our logs:

Note: Removing information specific to my employer and our systems.

01-26 15:21:54 [TP-Processor17] INFO  com].[/] - parameter=userName=nameValue65, nameValue65
01-26 15:21:54 [TP-Processor17] INFO  com].[/] - parameter=policyAccepted=true, true

This, of course, caused the end-point validation to error and disable user-registration.

Here's a representation of the code used to create the connection:

URL url = new URL("http://account-ws.domain.tld/register.action?responseType=json&userName=nameValue65&age=24&country=US&password1=Passw3rt&emailAddress=name@domain.tld&tosAccepted=true&policyAccepted=true");

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestMethod("GET");
urlc.setDoOutput(true);
urlc.setAllowUserInteraction(false);

PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.print(restEndPoint);
ps.close();

Perhaps(?) useful info:

  • The registration form submits to itself using POST, at which point we validate using Struts forms, and send the request to the web service using the values returned by the Struts form validation class. (These values are checked for accuracy once more before sending.)
  • Wireshark and log4j debug messages indicate that the URL sent to the web service is correct / what we would expect, with single values for each parameter.
  • The initiating form's post fields are named identically to the query keys sent with the web service request.

Please ask for more info if you find what's here to be insufficient.

Thank you in advance! :)

mO_odRing
  • 1,935
  • 3
  • 15
  • 19
  • What happen if you encode the values in url? i.e. name@domain.tld to name%40domain.tld – gigadot Jan 28 '11 at 23:27
  • Although I can't determine what you could be getting at with that particular approach, I gave it a try for giggles. URLEncode'ing each item doesn't make a difference. Also, it would be worth mentioning that that there are items which do not have special characters, such as `policyAccepted=true`--thus, would not be encoded--that are showing as 'duplicated' on the far end of the request. Thank you for taking time to reply. :) – mO_odRing Jan 29 '11 at 00:22
  • Can you set doOutput to false? I have read it somewhere that if dooutput is true, the request method will be changed to POST. That's might be the cause for having duplicated values for each parameter. – gigadot Jan 29 '11 at 01:03
  • And I am not sure why do you want to print something to output stream when you use GET. GET method is only for obtaining content from server. If you want to do POST, using setDoOutput(true) is enough. There is no need to setRequestMethod("POST"). – gigadot Jan 29 '11 at 01:06

1 Answers1

1

When you use GET method, the query string is added to URL string. The GET method is a default Http request method for HttpURLConnection. You do not need to explicitly set the request method to GET.

A GET method is used to obtain the content of the requested URL. You should not write to output stream of the GET connection.

If you want to use POST method, you can set it via setRequestMethod("POST") but I am not sure if you need to have setDoOutput(true) as well. However, the setDoOutput(true) will, by default, set request method to POST so you might as well ignore the setRequestMethod("POST"). If you want to write to output stream using POST, here is my previous answer of how to do it using HttpURLConnection.

It should be noted that when you do a POST (or PUT), the URL should not contain query part. Since you have a mixture of GET and POST, this might be the cause of your problem but I am not certain.

One possible case where you have to use both setRequestMethod and setDoOutput(true) is when you want to do a Http PUT.

Community
  • 1
  • 1
gigadot
  • 8,879
  • 7
  • 35
  • 51