import
java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import org.openqa.grid.web.servlet.handler.WebDriverRequest;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
public class correct {
public static String sendGet(String url, String param, String newurl, String post) {
HttpURLConnection connection;
String result2 = "";
BufferedReader in2 = null;
String urlNameString = url + "?" + param;
URL realUrl;
try {
realUrl = new URL(urlNameString);
String cookieVal = null;
String sessionId = "";
String key = null;
if (realUrl != null) {
try {
connection = (HttpURLConnection) realUrl.openConnection();
for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
if (key.equalsIgnoreCase("set-cookie")) {
cookieVal = connection.getHeaderField(i);
cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
sessionId = sessionId + cookieVal + ";";
}
}
System.out.println(sessionId);
} catch (MalformedURLException e) {
System.out.println("url can't connection");
return null;
} catch (IOException e) {
System.out.println(e.getMessage());
return null;
}
}
try {
URL NewURL = new URL(newurl);
connection = (HttpURLConnection) NewURL.openConnection();
if (realUrl != null) {
connection.setRequestProperty("Cookie", sessionId);
}
if (post != "") {
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.getOutputStream().write(post.getBytes());
connection.getOutputStream().flush();
connection.getOutputStream().close();
}
int responseCode = connection.getResponseCode();
int contentLength = connection.getContentLength();
if (responseCode != HttpURLConnection.HTTP_OK)
return (null);
in2 = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in2.readLine()) != null) {
result2 += line;
}
} catch (Exception e) {
return (null);
}
return result2;
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
return result2;
}