关于使用HTT
/*
* Copyright 2011 http://pvoutput.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pvoutput.integration.util;
import
org.apache.log4j.Logger;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.pvoutput.integration.Main;
public class WebClient
{
private static Logger LOGGER = Logger.getLogger(WebClient.class);
private WebClient()
{
}
public static HttpClient getWebClient()
{
HttpClient httpClient = new HttpClient();
httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
httpClient.setConnectTimeout(15000);
httpClient.setMaxRedirects(5);
httpClient.setMaxConnectionsPerAddress(5);
httpClient.setIdleTimeout(30000);
httpClient.setTimeout(30000);
try
{
httpClient.start();
}
catch (Exception e)
{
LOGGER.error("Could not start http client", e);
}
return httpClient;
}
public static ContentExchange getPVOutputGetExchange(String hostname, int port, String query, String sid, String key, String format)
{
return getPVOutputExchange(hostname, port, query, HttpMethods.GET, sid, key, format, null);
}
public static ContentExchange getPVOutputPostExchange(String hostname, int port, String query, String sid, String key, String format, String data)
{
return getPVOutputExchange(hostname, port, query, HttpMethods.POST, sid, key, format, data);
}
public static ContentExchange getPVOutputExchange(String hostname, int port, String query, String type, String sid, String key, String format, String data)
{
ContentExchange contentExchange = new ContentExchange();
contentExchange.setTimeout(15000);
if(HttpMethods.POST.equals(type))
{
contentExchange.setMethod(HttpMethods.POST);
if(data != null)
{
contentExchange.setRequestContent(new ByteArrayBuffer(data));
}
}
else
{
contentExchange.setMethod(type);
}
if(format != null)
{
contentExchange.setRequestHeader("User-Agent", "PVOutput/" + Main.VERSION + " (SystemId:" + sid + "; Inverter:" + format + ")");
}
else
{
contentExchange.setRequestHeader("User-Agent", "PVOutput/" + Main.VERSION);
}
contentExchange.setRequestHeader("X-Pvoutput-SystemId", sid);
contentExchange.setRequestHeader("X-Pvoutput-Apikey", key);
StringBuffer b = new StringBuffer();
b.append("http://").append(hostname).append(":").append(port).append(query);
LOGGER.info(">>> " + b.toString());
contentExchange.setURL(b.toString());
return contentExchange;
}
public static ContentExchange getExchange(String hostname, int port, String query, boolean secure)
{
ContentExchange contentExchange = new ContentExchange();