分享和关爱由此萌生,测试的人生会像流水和氧气 ,逐渐逐渐染绿了山河,染蓝了天空,萌生了飞鸟鱼虫,遍地都是生命。这是寒武纪带给我们的这份喜悦和希望。

已知网址和参数,获取cookie信息,打开另外一个网址及其参数的方法

上一篇 / 下一篇  2016-04-22 14:53:04 / 个人分类:Selenium/Eclipse/Java/Python

已知网址和参数,获取cookie信息,打开另外一个网址及其参数的方法
1:已知网址和这个网址所携带的参数
2:获取它的cookie值
3:打开另外一个网址,并且也提交相应的参数
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;

}

TAG: Cookie cookie 信息

 

评分:0

我来说两句

wchair

wchair

测试爱好者,数学爱好者,幻想爱好者,故事爱好者!

日历

« 2024-04-13  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 40552
  • 日志数: 32
  • 建立时间: 2007-11-13
  • 更新时间: 2022-11-29

RSS订阅

Open Toolbar