java httpurlconnection 发送cookie时,cookie要在Post前发送
2011-04-08 11:22
471 查看
public InputStream getStream(URL url,String post,URL cookieurl){
HttpURLConnection connection;
String cookieVal = null;
String sessionId = "";
String key=null;
if(cookieurl!=null){
try{
connection = (HttpURLConnection)cookieurl.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+";";
}
}
InputStream in = connection.getInputStream();
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 {
connection = (HttpURLConnection)url.openConnection();
//这个要写在Post前,否则会取不到值,原因我不知道
if(cookieurl!=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();
// System.out.println("Content length: "+contentLength);
if (responseCode != HttpURLConnection.HTTP_OK ) return(null);
InputStream in = connection.getInputStream();
return(in);
}
catch(Exception e) {
// System.out.println(e);
// e.printStackTrace();
return(null);
}
}
HttpURLConnection connection;
String cookieVal = null;
String sessionId = "";
String key=null;
if(cookieurl!=null){
try{
connection = (HttpURLConnection)cookieurl.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+";";
}
}
InputStream in = connection.getInputStream();
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 {
connection = (HttpURLConnection)url.openConnection();
//这个要写在Post前,否则会取不到值,原因我不知道
if(cookieurl!=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();
// System.out.println("Content length: "+contentLength);
if (responseCode != HttpURLConnection.HTTP_OK ) return(null);
InputStream in = connection.getInputStream();
return(in);
}
catch(Exception e) {
// System.out.println(e);
// e.printStackTrace();
return(null);
}
}
相关文章推荐
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- 谈谈Java利用原始HttpURLConnection发送POST数据
- Java使用HttpURLConnection发送Get及Post请求简单过程
- java基础——用HttpURLConnection向服务器发送POST请求
- Java利用HttpURLConnection发送post请求
- java发送post和get请求(1)---HttpURLConnection方式
- Java利用HttpURLConnection发送post请求
- Java利用HttpURLConnection发送post请求上传文件
- 谈谈Java利用原始HttpURLConnection发送POST数据
- Java: HttpURLConnection 模拟登录方法 (带cookie 的Post/Get)_20160908_七侠镇莫尛貝
- 谈谈Java利用原始HttpURLConnection发送POST数据
- Java利用HttpURLConnection发送post请求上传文件
- JAVA使用原始HttpURLConnection发送POST数据
- Java学习--HttpURLConnection发送post请求
- 谈谈Java利用原始HttpURLConnection发送POST数据
- Java: HttpURLConnection 模拟登录方法 (带cookie 的Post/Get)_20160908_七侠镇莫尛貝
- 谈谈Java利用原始HttpURLConnection发送POST数据
- Java使用HttpURLConnection发送POST和GET请求