java httpurlconnection 发送cookie时,cookie要在Post前发送   2 comments

  1. public InputStream getStream(URL url,String post,URL cookieurl){
  2. HttpURLConnection connection;
  3. String cookieVal = null;
  4. String sessionId = “”;
  5. String key=null;
  6. if(cookieurl!=null){
  7. try{
  8. connection = (HttpURLConnection)cookieurl.openConnection();
  9. for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++ ) {
  10. if (key.equalsIgnoreCase(“set-cookie”)) {
  11. cookieVal = connection.getHeaderField(i);
  12. cookieVal = cookieVal.substring(0, cookieVal.indexOf(“;”));
  13. sessionId = sessionId+cookieVal+”;”;
  14. }
  15. }
  16. InputStream in = connection.getInputStream();
  17. System.out.println(sessionId);
  18. }catch(MalformedURLException e){
  19. System.out.println(“url can’t connection”);
  20. return null;
  21. }catch(IOException e){
  22. System.out.println(e.getMessage());
  23. return null;
  24. }
  25. }
  26. try {
  27. connection = (HttpURLConnection)url.openConnection();
  28. //这个要写在Post前,否则会取不到值,原因我不知道
  29. if(cookieurl!=null){
  30. connection.setRequestProperty(“Cookie”, sessionId);
  31. }
  32. if(post!=””){
  33. connection.setDoOutput(true);
  34. connection.setRequestMethod(“POST”);
  35. connection.getOutputStream().write(post.getBytes());
  36. connection.getOutputStream().flush();
  37. connection.getOutputStream().close();
  38. }
  39. int responseCode = connection.getResponseCode();
  40. int contentLength = connection.getContentLength();
  41. // System.out.println(“Content length: “+contentLength);
  42. if (responseCode != HttpURLConnection.HTTP_OK ) return(null);
  43. InputStream in = connection.getInputStream();
  44. return(in);
  45. }
  46. catch(Exception e) {
  47. // System.out.println(e);
  48. // e.printStackTrace();
  49. return(null);
  50. }
  51. }

 

Posted 2011年03月19日 by gw8310 in java

2 responses to “java httpurlconnection 发送cookie时,cookie要在Post前发送

Subscribe to comments with RSS.

  1. java HttpURLConnection 登录网站 完整代码

    import java.io.*;
    import java.util.*;
    import java.net.*;

    public class WebTest {

    public static void main(String[] args) {

    System.out.println(“beging…”);
    DownLoadPages(“http://login.xiaonei.com/Login.do”, “d:/fileDown.txt”);
    // visit(“http://www.xiaonei.com”);
    System.out.println(“end.”);
    }

    public static void DownLoadPages(String urlStr, String outPath) {
    int chByte = 0;

    URL url = null;

    HttpURLConnection httpConn = null;

    InputStream in = null;

    FileOutputStream out = null;

    try {
    String post = “email=” + URLEncoder.encode(“e-mail”, “UTF-8”)
    + “&password=” + “password”;
    url = new URL(urlStr);

    httpConn = (HttpURLConnection) url.openConnection();

    //setInstanceFollowRedirects can then be used to set if
    //redirects should be followed or not and this should be used before the
    //connection is established (via getInputStream, getResponseCode, and other
    //methods that result in the connection being established).

    httpConn.setFollowRedirects(false);

    //inorder to disable the redirects
    httpConn.setInstanceFollowRedirects(false);

    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    httpConn.setRequestProperty(“User-Agent”,
    “Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)”);
    httpConn.setRequestProperty(“Content-Type”,
    “application/x-www-form-urlencoded”);

    //ok now, we can post it
    PrintStream send = new PrintStream(httpConn.getOutputStream());
    send.print(post);
    send.close();
    URL newURL = new URL(httpConn.getHeaderField(“Location”));
    System.out.println(“the URL has move to ”
    + httpConn.getHeaderField(“Location”));
    httpConn.disconnect();

    // OK, now we are ready to get the cookies out of the URLConnection
    String cookies = getCookies(httpConn);
    System.out.println(cookies);
    httpConn = (HttpURLConnection) newURL.openConnection();
    httpConn.setRequestProperty(“User-Agent”,
    “Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)”);
    httpConn.setRequestProperty(“Content-Type”,
    “application/x-www-form-urlencoded”);
    httpConn.setRequestProperty(“Cookie”, cookies);

    httpConn.setDoInput(true);
    in = httpConn.getInputStream();
    out = new FileOutputStream(new File(outPath));

    chByte = in.read();
    while (chByte != -1) {
    out.write(chByte);
    //System.out.println(chByte);
    chByte = in.read();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static String getCookies(HttpURLConnection conn) {
    StringBuffer cookies = new StringBuffer();
    String headName;
    for (int i = 7; (headName = conn.getHeaderField(i)) != null; i++) {
    StringTokenizer st = new StringTokenizer(headName, “; “);
    while (st.hasMoreTokens()) {
    cookies.append(st.nextToken() + “; “);
    }
    }
    return cookies.toString();
    }
    }
    char 和 wchar_t相互转化 | java 登录网站 cookie的设置

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: