Luís Tavares Luís Tavares - 1 month ago 5
HTTP Question

How to copy URL character values into string

I'm trying to store a value of a GET request into another variable.
When I try to do that with numbers, such a:

http://87.103.35.230/test.html?a=20&b=10


I store the values using the next method:

int valor1 = Integer.parseInt(streamAux.substring(pos1+2, pos2-1).trim());

int valor2 =Integer.parseInt(streamAux.substring(pos2+2, pos3-1).trim());


However, I want to do that with characters.

http://87.103.35.230/test.html?a=RS232&b=10


How do I do that? I tried establishing a string and giving it the same output like this:

String testx = "";
testx = streamAux.substring(pos11+22, pos22-11);


However, it does not work.

The streamAux object is a string which holds the GET request.
pos11 and pos22 come from this:

int pos11=streamAux.indexOf("=R");
int pos22=streamAux.indexOf(",");
int pos33=streamAux.indexOf("HTTP/1.1");


Can somebody give me a help?

Answer

Here it is. Try it.

i=streamAux.indexOf('a=')+1;
j=streamAux.indexOf('&',i);
string a=streamAux.subString(i,j);
i=streamAux.indexOf('b=',j)+1;
int b=Integer.parseInt(streamAux.subString(i))

a is received as string keeping 'RS' in it. b is integer. Hope it is what you want.