Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this query string to send a http request via a URI object, but the URI object reformats my query string, including the parameters.

Expected: http://myDomain.com:80/ebl/remote/95b0cd58477517af33483dd699c5dbef/v2/cart/addProduct?cid=14305618-c6c0-475a-af70-c8055bc3eeec&pid=1000000023120&qty=1

Real result: http://myDomain.com:80/ebl/remote/95b0cd58477517af33483dd699c5dbef/v2/cart/addProduct%3Fcid=14305618-c6c0-475a-af70-c8055bc3eeec&pid=1000000023120&qty=1?

(the %3F and the ? are wrong)

The code:

String queryString = "";
queryString += "/ebl/remote";
queryString += "/" + getToken();        
queryString += "/v2/cart/addProduct?";

queryString += "cid=" + getCartID();

if (getItemID() != null) {
    queryString += "&pid=" + getItemID();
}

queryString += "&qty=1";

URI uri = null;
try {
    uri = new URI("http", null, getDomain(), 80, queryString, "", null);
} catch (Exception e) {}

HttpClient httpclient = new DefaultHttpClient();

try {
    HttpGet httpget = new HttpGet(uri);
    System.out.println("Add product request: " + httpget.getURI());

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);
} catch (Exception e) {}

Can someone help me fix this please?

share|improve this question
1  
You are using the constructor wrong. Your path should go in the path variable and your query string should go in the queryString variable. The URI is escaping the query because you are telling it that it's part of the path. – Boris the Spider 22 hours ago

1 Answer

You're adding the "query" part to the path. You want

queryString /* misnamed */ += "/v2/cart/addProduct";
String query = "?...";
...
uri = new URI("http", null, getDomain(), 80, queryString, query, null);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.