请教一个HTTPClient问题

njsoftcn 2009-12-09 09:25:23
请教高人,我刚学java网络编程不久,想做一个“人人网”共享程序,但是一直不能成功,想请帮我看看我的程序错在哪里,代码如下:
用的是httpClient 3.1工具包
public class httpLogin {

final String LOGON_SITE = "www.renren.com";
final int LOGON_PORT = 80;
private Calendar calendar;

private HttpClient client;

private String tsc;

public httpLogin() {

client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod authget = new GetMethod("/");

try {
client.executeMethod(authget);
} catch (IOException ex) {
}
System.out.println("Login form get: " + authget.getStatusLine().toString());
authget.releaseConnection();
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] initcookies = cookiespec.match(
LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
System.out.println("Initial set of cookies:");//打印Cookies
if (initcookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < initcookies.length; i++) {
System.out.println("- " + initcookies[i].toString());
}
}

}

public boolean login(){//登录
NameValuePair nvp[] = {
new NameValuePair("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"),
new NameValuePair("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
new NameValuePair("Accept-Language",
"zh-cn,en-us;q=0.7,zh;q=0.3"),
new NameValuePair("Accept-Charset",
"gb2312,utf-8;q=0.7,*;q=0.7"),
new NameValuePair("email", "tyhjaocn200901"),
new NameValuePair("password", "87189118")
};

PostMethod post = new PostMethod("http://passport.renren.com/PLogin.do");
post.setRequestBody(nvp);
try {
client.executeMethod(post);
int status = post.getStatusCode();
if (status == 302) {
calendar = Calendar.getInstance();
System.out.println("\n登录成功! "+calendar.get(calendar.YEAR)+"年"+(calendar.get(calendar.MONTH)+1)+"月"+calendar.get(calendar.DAY_OF_MONTH)+"日 "+calendar.get(calendar.HOUR_OF_DAY)+":"+calendar.get(calendar.MINUTE)+":"+calendar.get(calendar.SECOND)+"\n");
return true;
}
} catch (Exception e) {
if (e.getMessage().contains("Read")) {
System.out.println("登录超时!");
System.exit(1);
}
} finally {
post.releaseConnection();
}
System.out.println("登录失败\n");
return false;
}

public void toShare(){//跳转到共享页面
GetMethod getMethod = new GetMethod("http://share.renren.com/");
try {
client.executeMethod(getMethod);
int status = getMethod.getStatusCode();
if (status == 200) {
System.out.println("OK!");
}
} catch (IOException ex) {
} finally {
getMethod.releaseConnection();
}

this.cookies();
this.sendLink();
}

public void cookies(){//设置cookies
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] initcookies = cookiespec.match(
LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
System.out.println("Initial set of cookies:");
if (initcookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < initcookies.length; i++) {
System.out.println("- " + initcookies[i].toString());
}
}

}

public void sendLink() {//人人网分享的第一个共享页面提交
NameValuePair nvp[] = {
new NameValuePair("action", "sharelink"),
new NameValuePair("weblink","http://www.tyhjao.cn"),
};
PostMethod post = new PostMethod("http://share.renren.com/share/ShareLink.do");
post.setRequestBody(nvp);
try {
client.executeMethod(post);
int status = post.getStatusCode();
if (status == 200) {
System.out.println("\n跳转成功\n");
}
InputStream input = post.getResponseBodyAsStream();
String cont = readContent(input);
int start = 0;
tsc = cont.substring(start = (cont.lastIndexOf("tsc") + 12),cont.indexOf("\"", start));
System.out.println(tsc);
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
this.cookies();
}
lastLink();
}

public void lastLink() {//分享提交
NameValuePair nvp[] = {
new NameValuePair("link", "http://www.tyhjao.cn"),
new NameValuePair("pic", ""),
new NameValuePair("type", "6"),
new NameValuePair("fromno", "0"),
new NameValuePair("fromname", ""),
new NameValuePair("fromuniv", ""),
new NameValuePair("summary", ""),
new NameValuePair("referer", ""),
new NameValuePair("tsc", tsc),
new NameValuePair("action", "add"),
new NameValuePair("body", "001"),
new NameValuePair("auth", "99"),
new NameValuePair("title", "0001"),
};
PostMethod post = new PostMethod(
"http://share.renren.com/share/ajaxSaveShare.do");
post.setRequestBody(nvp);
try {
client.executeMethod(post);
int status = post.getStatusCode();
if (status == 302||status==301) {
Header locationHeader = post.getResponseHeader("location");
String location = null;
if(locationHeader!=null){
location = locationHeader.getValue();
System.out.println("The page was redirectes to:"+location);
}
System.out.println("\n share is successful1 \n");
} else if(status == 200){
System.out.println("共享成功!");
}
else {
System.out.println("field1");//13305166710
}
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
this.cookies();
}
}

public static void main(String[] args) throws Exception{

httpLogin login = new httpLogin();
login.login();
login.toShare();

}

private static String readContent(InputStream is) throws Exception {//将输入流转换成html
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return new String(sb.toString().getBytes(), "utf-8");
}
}
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl3450341 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 njsoftcn 的回复:]
为什么我的sendLink()函数提交能成功,而lastLink()就不成功呢?
[/Quote]

....这么多代码 帮你顶下
njsoftcn 2009-12-09
  • 打赏
  • 举报
回复
为什么我的sendLink()函数提交能成功,而lastLink()就不成功呢?
njsoftcn 2009-12-09
  • 打赏
  • 举报
回复
lastlink();函数哪里是302,要是共享成功的话应该是200
道光2008 2009-12-09
  • 打赏
  • 举报
回复
什么错误,异常贴出来

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧