`
gstarwd
  • 浏览: 1491886 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

httpclient(校内网)

阅读更多
Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> package com.lihan.dao; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.PostMethod; public final class LoginXiaonei { public static void main(String[] a) { HttpClient client = new HttpClient(); PostMethod post = new PostMethod( " http://login.xiaonei.com/Login.do " ); post.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " ); client.getParams().setContentCharset( " utf-8 " ); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); NameValuePair[] data = { new NameValuePair( " email " , " ********@163.com " ), new NameValuePair( " password " , " ****** " ) }; post.setRequestBody(data); try { client.executeMethod(post); Cookie [] cookies = client.getState().getCookies(); InputStream stream = post.getResponseBodyAsStream(); writeStream(stream); stream.close(); post.releaseConnection(); post = new PostMethod( " http://msg.xiaonei.com/message/send.do " ); post.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " ); data = new NameValuePair[]{ new NameValuePair( " ids[] " , " 227949683 " ) , new NameValuePair( " subject " , " hahahahaha " ) , new NameValuePair( " message " , " 0 " ) , }; post.setRequestBody(data); client.executeMethod(post); stream = post.getResponseBodyAsStream(); writeStream(stream); stream.close(); post.releaseConnection(); } catch (HttpException ex) { Logger.getLogger(XiaoNeiBlog. class .getName()).log(Level.SEVERE, null , ex); } catch (IOException ex) { Logger.getLogger(XiaoNeiBlog. class .getName()).log(Level.SEVERE, null , ex); } } private static void writeStream(InputStream stream) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(stream, " utf-8 " )); String line = null ; for (line = reader.readLine();line != null ;line = reader.readLine()) { System.out.println(line); } reader.close(); } }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics