android 发送cookie
能够得到cookie,发送时却总是报错,不知为什么程序如下:
URL url=new URL(httpUrl);//http 为网址
HttpURLConnection con =(HttpURLConnection)url.openConnection();
String cookieVal = null;
String key=null;
String sessionId="";
//得到cookie
for (int i = 1; (key = con.getHeaderFieldKey(i)) != null; i++ ) {
if (key.equalsIgnoreCase("set-cookie")) {
cookieVal = con.getHeaderField(i);
cookieVal = cookieVal.substring(0, cookieVal.indexOf(";")); sessionId=sessionId+cookieVal+";";
}}
System.out.println("cookie:"+sessionId);
//以上程序可以得到cookie
//下面发送时却总是报错
con.setRequestProperty("Cookie",sessionId);//这就是出错的地方
不知为什么,请教高手 --------------------编程问答-------------------- 自己顶一下,已解决,找到了一种办法
URL url=new URL(httpUrl);//http 为网址
HttpURLConnection con =(HttpURLConnection)url.openConnection();
HttpURLConnection con2 =(HttpURLConnection)url.openConnection();
String cookieVal = null;
String key=null;
String sessionId="";
//得到cookie
for (int i = 1; (key = con2.getHeaderFieldKey(i)) != null; i++ ) {
if (key.equalsIgnoreCase("set-cookie")) {
cookieVal = con2.getHeaderField(i);
cookieVal = cookieVal.substring(0, cookieVal.indexOf(";")); sessionId=sessionId+cookieVal+";";
}}
System.out.println("cookie:"+sessionId)
con.setRequestProperty("cookie",sessionId );
InputStream is = con.getInputStream();//得到网络返回的输入流
result = readData(is, "utf-8");
con.disconnect();
System.out.println(result); --------------------编程问答--------------------
补充:Java , Web 开发