All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.toolbox.model.HttpInvokeResult Maven / Gradle / Ivy

/*
 * @(#)HttpInvokeResult.java	2020-8-5
 *
 * Copyright (c) 2020. All Rights Reserved.
 *
 */

package com.github.javaclub.toolbox.model;

import java.io.Serializable;
import java.net.HttpURLConnection;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * HttpInvokeResult
 *
 * @author Gerald Chen
 * @version $Id: HttpInvokeResult.java 2020-8-5 13:31:04 Exp $
 */
public final class HttpInvokeResult implements Serializable {

	private static final long serialVersionUID = -4345580169599472262L;
	
	/**
	 * http请求响应代码 
	 */
	private int responseCode;
	
	/**
	 * 实际响应内容, 当 responseCode = 302 时, 为302跳转之后响应内容
	 */
	private String responseText;
	
	/**
	 * http 请求Url
	 */
	private String requestUrl;
	
	/**
	 * 跳转Url, 此时 responseCode = 302
	 */
	private String redirectUrl;
	
	public HttpInvokeResult() {}
	
	public HttpInvokeResult(String url) {
		this.requestUrl = url;
	}
	
	public HttpInvokeResult(int responseCode, String responseText) {
		this.responseCode = responseCode;
		this.responseText = responseText;
	}

	public int getResponseCode() {
		return responseCode;
	}

	public void setResponseCode(int responseCode) {
		this.responseCode = responseCode;
	}

	public String getResponseText() {
		return responseText;
	}

	public void setResponseText(String responseText) {
		this.responseText = responseText;
	}
	
	public String getRequestUrl() {
		return requestUrl;
	}

	public void setRequestUrl(String url) {
		this.requestUrl = url;
	}

	public String getRedirectUrl() {
		return redirectUrl;
	}

	public void setRedirectUrl(String redirectUrl) {
		this.redirectUrl = redirectUrl;
	}

	public boolean isHttpOK() {
		return getResponseCode() == HttpURLConnection.HTTP_OK;
	}
	
	public  T parseResponse(Class clazz) {
		if(null != responseText) {
			try {
				return JSONObject.parseObject(responseText, clazz);
			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy