解决方案 »
- SQL分组问题
- 有没有今年拿到淘宝实习offer的......找同伴.....
- 为何 Struts2 在运行action类的时候不会在console窗口输出错误信息?
- 怎样弹出日期和时间小窗口
- 怎么安装mysql gui tools
- 我用了struts validator框架,如何在extends ValidatorForm的表单里保存信息到request里面?
- 请问如何实现JSP页面的翻页功能~~~多谢
- 植“树”造林----树型菜单大展示
- ycats(加菲猫)我刚才按照你的方法去做,不能够实现,能够功能?
- 急急急!!!JSP上传图片的问题
- struts2中的web.xml配置问题
- jsp工程运行不了问题
参数:
username:mememomo
password:******
chkcode:5354
login:1
HashMap<String, String> params=new HashMap<String, String>();
params.put("login_url", "http://pt.3g.qq.com/s?aid=nLogin");
params.put("sidtype", "1");
params.put("loginTitle", "手机腾讯网");
params.put("bid", "0");
params.put("qq", qq);
params.put("pwd", password);
params.put("loginType", "1");
try {
String response=WebUtils.doPost(QQ_LOGIN_URL, params, 0, 0);
int sidIndex=response.indexOf("sid");
SID=response.substring(sidIndex+4, sidIndex+28);
} catch (IOException e) {
e.printStackTrace();
}
return SID;
}WebUtils类的doPost方法:
public static String doPost(String url, Map<String, String> params,int connectTimeout,int readTimeout) throws IOException {
String query=buildQuery(params, DEFAULT_CHARSET);
byte[] content=null;
if(query!=null){
content=query.getBytes(DEFAULT_CHARSET);
}
return doPost(url, CTYPE, content, connectTimeout, readTimeout);
} public static String doPost(String url, String ctype, byte[] content,int connectTimeout,int readTimeout) throws IOException {
HttpURLConnection conn = null;
OutputStream out = null;
String rsp = null;
try {
try{
conn = getConnection(new URL(url), METHOD_POST, ctype);
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
out = conn.getOutputStream();
out.write(content);
rsp = getResponseAsString(conn);
}catch(IOException e){
throw e;
}
}finally {
if (out != null) {
out.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rsp;
} private static HttpURLConnection getConnection(URL url, String action, String ctype)
throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(action);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Accept", "text/xml,text/javascript,text/html");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Content-Type", ctype);
return conn;
} private static String getStreamAsString(InputStream stream, String charset) throws IOException {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
StringWriter writer = new StringWriter();
char[] chars = new char[256];
int count = 0;
while ((count = reader.read(chars)) > 0) {
writer.write(chars, 0, count);
}
return writer.toString();
} finally {
if (stream != null) {
stream.close();
}
}
}
看了下paramStr的拼凑
String paramStr = "?"+ "&userName=" + userName + "&password=" + password + "&chkcode=" + chkcode+"&login=1"; //验证码和login参数直接少了个&导致你验证码变成了你输入的1386login=1了所以返回了验证码出错。楼主把这个&符号加上再试试吧
String paramStr = "?"+ "&userName=" + userName + "&password=" + password + "&chkcode=" + chkcode+"&login=1";
第一个参数后面不用跟&正确的格式应该是
String paramStr = "?"+ "userName=" + userName + "&password=" + password + "&chkcode=" + chkcode+"&login=1"; //参数名userName前面的&去掉