Exemplo n.º 1
0
  /**
   * get请求URL
   *
   * @param url
   * @throws AppException
   */
  private static InputStream http_get(AppContext appContext, String url) throws AppException {
    // System.out.println("get_url==> "+url);
    /*		String cookie = getCookie(appContext);
    String userAgent = getUserAgent(appContext);*/

    HttpClient httpClient = null;
    GetMethod httpGet = null;

    String responseBody = "";
    int time = 0;
    do {
      try {
        httpClient = getHttpClient();
        //		httpGet = getHttpGet(url/*, cookie, userAgent*/);
        httpGet = getHttpGet(url, appContext.mCookies, "");

        /*httpGet = getHttpGet(url, cookie, userAgent);*/
        int statusCode = httpClient.executeMethod(httpGet);
        if (statusCode != HttpStatus.SC_OK) {
          throw AppException.http(statusCode);
        }
        responseBody = httpGet.getResponseBodyAsString();
        // System.out.println("XMLDATA=====>"+responseBody);
        break;
      } catch (HttpException e) {
        time++;
        if (time < RETRY_TIME) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e1) {
          }
          continue;
        }
        // 发生致命的异常,可能是协议不对或者返回的内容有问题
        e.printStackTrace();
        throw AppException.http(e);
      } catch (IOException e) {
        time++;
        if (time < RETRY_TIME) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e1) {
          }
          continue;
        }
        // 发生网络异常
        e.printStackTrace();
        throw AppException.network(e);
      } finally {
        // 释放连接
        httpGet.releaseConnection();
        httpClient = null;
      }
    } while (time < RETRY_TIME);

    responseBody = responseBody.replaceAll("\\p{Cntrl}", "");
    /*if(responseBody.contains("result") && responseBody.contains("errorCode") && appContext.containsProperty("user.uid")){
    	try {
    		Result res = Result.parse(new ByteArrayInputStream(responseBody.getBytes()));
    		if(res.getErrorCode() == 0){
    			appContext.Logout();
    			appContext.getUnLoginHandler().sendEmptyMessage(1);
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }*/
    return new ByteArrayInputStream(responseBody.getBytes());
  }
Exemplo n.º 2
0
  /**
   * 公用post方法
   *
   * @param url
   * @param params
   * @param files
   * @throws AppException
   */
  private static String _post(
      AppContext appContext, String url, Map<String, Object> params, Map<String, File> files)
      throws AppException {

    /*String cookie = getCookie(appContext);
    String userAgent = getUserAgent(appContext);*/

    HttpClient httpClient = null;
    PostMethod httpPost = null;

    // post表单参数处理
    int length = (params == null ? 0 : params.size()) + (files == null ? 0 : files.size());
    Part[] parts = new Part[length];
    int i = 0;
    if (params != null)
      for (String name : params.keySet()) {
        parts[i++] = new StringPart(name, String.valueOf(params.get(name)), UTF_8);
        // System.out.println("post_key==> "+name+"    value==>"+String.valueOf(params.get(name)));
      }
    if (files != null)
      for (String file : files.keySet()) {
        try {
          parts[i++] = new FilePart(file, files.get(file));
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
        // System.out.println("post_key_file==> "+file);
      }

    String responseBody = "";
    int time = 0;
    do {
      try {
        httpClient = getHttpClient();
        httpPost = getHttpPost(url /*, cookie, userAgent*/);
        httpPost.setRequestEntity(new MultipartRequestEntity(parts, httpPost.getParams()));
        int statusCode = httpClient.executeMethod(httpPost);
        if (statusCode != HttpStatus.SC_OK) {
          throw AppException.http(statusCode);
        } else if (statusCode == HttpStatus.SC_OK) {
          /*Cookie[] cookies = httpClient.getState().getCookies();
          String tmpcookies = "";
          for (Cookie ck : cookies) {
              tmpcookies += ck.toString()+";";
          }*/
          // 保存cookie
          /*if(appContext != null && tmpcookies != ""){
          	appContext.setProperty("cookie", tmpcookies);
          	appCookie = tmpcookies;
          }*/
        }
        responseBody = httpPost.getResponseBodyAsString();

        // System.out.println("XMLDATA=====>"+responseBody);
        break;
      } catch (HttpException e) {
        time++;
        if (time < RETRY_TIME) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e1) {
          }
          continue;
        }
        // 发生致命的异常,可能是协议不对或者返回的内容有问题
        e.printStackTrace();
        throw AppException.http(e);
      } catch (IOException e) {
        time++;
        if (time < RETRY_TIME) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e1) {
          }
          continue;
        }
        // 发生网络异常
        e.printStackTrace();
        throw AppException.network(e);
      } finally {
        // 释放连接
        httpPost.releaseConnection();
        httpClient = null;
      }
    } while (time < RETRY_TIME);

    /*  responseBody = responseBody.replaceAll("\\p{Cntrl}", "");*/
    /*if(responseBody.contains("result") && responseBody.contains("errorCode") && appContext.containsProperty("user.uid")){
    	try {
    		Result res = Result.parse(new ByteArrayInputStream(responseBody.getBytes()));
    		if(res.getErrorCode() == 0){
    			appContext.Logout();
    			appContext.getUnLoginHandler().sendEmptyMessage(1);
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
          return new ByteArrayInputStream(responseBody.getBytes());*/

    return responseBody;
  }