使用java检查通过代理发出的请求的状态代码

时间:2016-09-08 12:21:01

标签: java httpresponse http-response-codes

我正在尝试使用Java使用代理发出http请求,并在整数变量中获取响应代码。

我使用的方法是:

const stateDischarge = {
    active: {
        display: 'inherit'
    },
    inactive: {
        display: 'none'
    }
};


var Preferences = React.createClass({
.
.
.

activeToggle: function (panel) {

    event.stopPropagation();  //  <= this is there I am trying the suggestion. but not working, so trying something else.

    switch (panel) {
        case "Demographics":
            this.setState({
                activeDemographics: !this.state.activeDemographics
            });
            break;
        case "Admission":
            this.setState({
                activeAdmission: !this.state.activeAdmission
            });
            break;
        case "Discharge":
            this.setState({
                activeDischarge: !this.state.activeDischarge
            });
            break;
        default:
            //Statements executed when none of the values match the value of the expression
            break;
    }
},
.
.
.
const stateDischarge = this.state.activeDischarge ? styles.active : styles.inactive;


    render: function() {

    <div className="panel panel-info">
        <div className="panel-heading"  onClick={() => this.activeToggle("Discharge")}>
            <h4 className="panel-title pull-left">
                <a data-toggle="collapse" data-parent="#accordion">Discharge</a>
            </h4>
            <div className="pull-right">
                <label className="checkbox-inline"><input type="checkbox" value=""/>Admission</label>
                <label className="checkbox-inline"><input type="checkbox" value=""/>Non-Admission</label>
            </div>
            <div className="clearfix"></div>
        </div>
        <div id="collapseOne" className="panel-collapse" style={stateDischarge}>
            <div className="panel-body">
                <div className="col-sm-6 col-md-6">

                    //Content in Accordion pannel                   

                </div>
            </div>
        </div>
    </div>

    }
});
module.exports = Preferences;

我正在传递代理参数,但我不确定在发出请求时如何使用它。我试图在线查找资源,但无法找到合适的解决方案。

我尝试了以下解决方案,但不确定如何在此处获取响应代码:

public static int getResponseCode(String urlString, Proxy p) throws MalformedURLException, IOException{
URL url = new URL(urlString);
HttpResponse urlresp = new DefaultHttpClient().execute(new HttpGet(urlString));
int resp_Code = urlresp.getStatusLine().getStatusCode();
return resp_Code;
}

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

URL url = new URL("http://www.myurl.com");

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.com", 8080));

// IF NEED AUTH         
//      Authenticator authenticator = new Authenticator() {
//          public PasswordAuthentication getPasswordAuthentication() {
//              return (new PasswordAuthentication("username",
//                      "password".toCharArray()));
//          }
//      };
//      Authenticator.setDefault(authenticator);

        HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
        conn.setRequestMethod("GET");
        conn.connect();

        int code = conn.getResponseCode();

        System.out.println(code);