Author Message
Athron
Joined: Nov 4, 2013
Messages: 40
Offline
Hi Guys,

Is there a code we can used in servlet to run this:

http://10.226.137.7:9020/?CSP_Txid=CHAT09175017588201107281000&SMS_MsgTxt=Sorry%2c%20there%20are%20no%20available%20live%20agent%20as%20of%20the%20moment.&SUB_R_Mobtel=09175017588&SUB_C_Mobtel=09175017588&SMS_SourceAddr=1234

The output of this is SUCCEEDED.

We need to get the output result when you run the link.

Thanks,
Anthony
WilsonYu
Joined: Nov 6, 2013
Messages: 3950
Offline
You can try something like this:

URL url= new URL("http://10.226.137.7:9020/?CSP_Txid=CHAT09175017588...9175017588&SMS_SourceAddr=1234")

URLConnection urlConnection = url.openConnection();
urlConnection.connect();

InputStream inputStream = (InputStream)urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String response = reader.readLine();
RossYakulis
Joined: Nov 6, 2013
Messages: 2652
Offline
One way it to use the Java HTTP methods:

public static void main(String[] args) {
try {
URL x = new URL("http://www.google.com");
URLConnection y = x.openConnection();
InputStream in = y.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String s;
do {
s = br.readLine();
if (s != null) {
System.out.println(s);
}
} while (s != null);
} catch (Exception e) {
e.printStackTrace();
}
}



You could also use the Apache Commons Httpclient:


public void servletImplementation() {
try {

URI url = new URI("http://www.google.com", false);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
HttpMethod method = new GetMethod();
method.setURI(url);
client.executeMethod(method);
if (method.getStatusCode() > 399) {
throw new HttpException(method.getStatusLine().toString());
}
Header[] headers = method.getResponseHeaders();
for (Header header : headers) {
System.out.println(header.getName() + ":" + header.getValue());
}
String responseBody = method.getResponseBodyAsString();

System.out.println(responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
Athron
Joined: Nov 4, 2013
Messages: 40
Offline
Thanks Guys!!
Athron
Joined: Nov 4, 2013
Messages: 40
Offline
Hi Ross,

We tried to used the following code:
public void servletImplementation() {
02 try {
03
04 URI url = new URI("http://www.google.com", false);
05 HttpClient client = new HttpClient();
06 client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
07 HttpMethod method = new GetMethod();
08 method.setURI(url);
09 client.executeMethod(method);
10 if (method.getStatusCode() > 399) {
11 throw new HttpException(method.getStatusLine().toString());
12 }
13 Header[] headers = method.getResponseHeaders();
14 for (Header header : headers) {
15 System.out.println(header.getName() + ":" + header.getValue());
16 }
17 String responseBody = method.getResponseBodyAsString();
18
19 System.out.println(responseBody);
20 } catch (Exception e) {
21 e.printStackTrace();
22 }
23 }

However how can we pass the result of variable s to my project?

Thanks!!!
RossYakulis
Joined: Nov 6, 2013
Messages: 2652
Offline

IVariableField field = mySession.getVariableField(IProjectVariables.MYVAR);
field.setValue(responseBody);


Assuming "MYVAR" is an existing variable.
Go to:   
Mobile view