Java code
- Public
Inputstream getstream (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. inclusignorecase (
"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 ();
- // This should be written before post; otherwise, the value will not be obtained, because I do not know
- 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
);
- }
- }
From: http://stephenjqj.javaeye.com/blog/477194