/** * 发上服务器的文件 * * @param file * @return */ private String sendHttpRequest(File file) { String string = null; PostMethod filePost = new PostMethod(targetURL); try { Part[] parts = { new FilePart("userFile.file", targetFile), new StringPart(paramType, param, "utf-8") }; HttpMethodParams params = filePost.getParams(); params.setContentCharset("utf-8"); filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(50000); Log.v("filePost", filePost.toString()); Log.v("param", param); Log.v("paramType", paramType); int status = httpClient.executeMethod(filePost); // TODO Log.v("status--->", status + ""); InputStream in = filePost.getResponseBodyAsStream(); byte[] readStream = readStream(in); string = new String(readStream); } catch (Exception ex) { ex.printStackTrace(); } finally { filePost.releaseConnection(); } return string; }
/** * post * * @param url * @param params * @param token * @return * @throws ApiException */ public static String post(String url, PostParameter[] params, String token) throws IOException { PostMethod postMethod = new PostMethod(url); for (int i = 0; i < params.length; i++) { postMethod.addParameter(params[i].getName(), params[i].getValue()); } HttpMethodParams param = postMethod.getParams(); param.setContentCharset(DEFAULTCHAESET); return httpRequest(postMethod, token); }
public Response post(String url, PostParameter[] params, Boolean WithTokenHeader, String token) throws WeiboException { log("Request:"); log("POST" + url); PostMethod postMethod = new PostMethod(url); for (int i = 0; i < params.length; i++) { postMethod.addParameter(params[i].getName(), params[i].getValue()); } HttpMethodParams param = postMethod.getParams(); param.setContentCharset("UTF-8"); return httpRequest(postMethod, WithTokenHeader, token); }
public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException { HttpClient client = new HttpClient(); NameValuePair[] nameValuePairs = new NameValuePair[4]; nameValuePairs[0] = new NameValuePair("apikey", apikey); nameValuePairs[1] = new NameValuePair("tpl_id", String.valueOf(tpl_id)); nameValuePairs[2] = new NameValuePair("tpl_value", tpl_value); nameValuePairs[3] = new NameValuePair("mobile", mobile); PostMethod method = new PostMethod(URI_TPL_SEND_SMS); method.setRequestBody(nameValuePairs); HttpMethodParams param = method.getParams(); param.setContentCharset(ENCODING); client.executeMethod(method); return method.getResponseBodyAsString(); }
// 授权,生成access_token // 使用情形:①程序初始化;②每隔一天左右重新授权access_token public static void generate() { initAccountInfo(); accessToken.clear(); logger.info("用户授权中..."); try { // https://api.weibo.com/oauth2/authorize?client_id=750123511&redirect_uri=https://api.weibo.com/oauth2/default.html&response_type=code String url = "https://api.weibo.com/oauth2/authorize"; String redirectUri = "https://api.weibo.com/oauth2/default.html"; for (int i = 0; i < accountInfo.size(); i++) { // 获取应用的信息 clientId = WeiboConfig.getValue("client_ID"); clientSecret = WeiboConfig.getValue("client_SERCRET"); // 构造授权的url参数 PostMethod postMethod = new PostMethod(url); postMethod.addParameter("client_id", clientId); postMethod.addParameter("redirect_uri", redirectUri); postMethod.addParameter("userId", accountInfo.get(i).getUserId()); postMethod.addParameter("passwd", accountInfo.get(i).getPasswd()); postMethod.addParameter("isLoginSina", "0"); postMethod.addParameter("action", "submit"); postMethod.addParameter("response_type", "code"); HttpMethodParams param = postMethod.getParams(); param.setContentCharset("UTF-8"); // 伪造头部域信息 List<Header> headers = new ArrayList<Header>(); headers.add( new Header( "Referer", "https://api.weibo.com/oauth2/authorize?client_id=" + clientId + "&redirect_uri=" + redirectUri + "&from=sina&response_type=code")); headers.add(new Header("Host", "api.weibo.com")); headers.add( new Header( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0")); // 发送HTTP请求 HttpClient client = new HttpClient(); client.getHostConfiguration().getParams().setParameter("http.default-headers", headers); client.executeMethod(postMethod); // 获取授权响应 int status = postMethod.getStatusCode(); if (status == 302) { Header location = postMethod.getResponseHeader("location"); if (location != null) { String retUrl = location.getValue(); int begin = retUrl.indexOf("code="); int end = retUrl.length(); String code = retUrl.substring(begin + 5, end); if (code != null) { Oauth oauth = new Oauth(); String token = oauth.getAccessTokenByCode(code).getAccessToken(); accessToken.add(token); logger.info("第" + (i + 1) + "个access_token:" + token); } } } else { logger.error("第" + (i + 1) + "个用户授权失败了!"); } } } catch (Exception e) { e.printStackTrace(); logger.error("授权发生异常!"); } }