0

I am using HttpUrlConnection to post a query to a webpage. The web page take my request and does another posting. For example: www.unknown.com

URL url = new URL("http://www.unknown.com"); //$NON-NLS-1$ urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setReadTimeout(15*1000); urlConnection.setDoInput(true); urlConnection.setDoOutput (true); urlConnection.setUseCaches (false); urlConnection.setAllowUserInteraction(true); out = new OutputStreamWriter(urlConnection.getOutputStream()); string content = "var1=5&var2=0&var3=6"; //$NON-NLS-1$ out.write(content); out.flush(); out.close(); 

The code is working without problem and I am getting the html code. The problem is that the webpage is proceeding another HTTP "POST" method when it is taking my request. For example after my request the URI is: http://www.unknown.com?var1=5&var2=0&var3=6&var4=1500

I need to get the value of "var4" in my code and I can not find any solution for this. HttpUrlConnection.getUrl() returns just the address http://www.unknown.com! Have anybody a suggestion? Thanks.

1 Answer 1

1

How about using "getQuery()" to retrieve the Query String.

String query = url.getQuery(); 

This will give you the the query part of this URL.

Use StringTokenizer to separate the parameters. (You will have to apply StringTokenizer twice.)

First get tokens from query string which are separated by "&". This will return "val1=5" , "val4=1500", etc.

To the above tokens apply StrinTokenizer once again. This time retrieve tokens separated by the "=". Now iterate through this, the first token will be the parameter name "val4", the second token will be the value "1500".

StringTokenizer st = new StringTokenizer(query,"&",false); //query is from getQuery() while (st.hasMoreElements()) { // First Pass to retrive the "parametername=value" combo String paramValueToken = st.nextElement().toString(); StringTokenizer stParamVal = new StringTokenizer(paramValueToken, "=", false ); int i = 0; while (stParamVal.hasMoreElements()) { //Second pass to separate the "paramname" and "value". // 1st token is param name // 2nd token is param value String separatedToken = stParamVal.nextElement().toString(); if( i== 0) { //This indicates that it is the param name : ex val4,val5 etc String paramName = separatedToken; } else { // This will hold value of the parameter String paramValue = separatedToken; } i++; } } 

URL getQuery() API Documentation

http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

Sign up to request clarification or add additional context in comments.

6 Comments

No, I am not using Servlets/JSP. It is an android application and I am not sure that if it is possible to use JSP in android applications.
@Govan edited my answer. USed a different approach to retrieve the Query parameters using the URL API and Strintokenizer.
Thanks for you care. I checked the url and the quey when I debugged the code. As I said the web page is proceeding its own HTTP-post when I am sending my request. "Var4" is not with the query string whichI am sending. the sever is doing its post and adds the var4 to query string. The query part of the URL didn't contain "Var4" after request when I debugged the code. I guess that getQuery() returns that part in URL. With other words I am getting the right HTML-page but I am not getting the right URL.
@Govan Each request is only valid to the first page that is invoked, the query string will not get automatically passed on to the second page. If you need the parameters in the second page, recreate the query string in the and pass it once again.
It is the problem, I don't have the value of "Var4" in the example and therefore I can not recreate the query string. May be I should ask my question like this: Is there any way to get the query string made by a webpage? The response is taking the body but how I can get the URL?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.