本文整理汇总了Java中com.github.kevinsawicki.http.HttpRequest.form方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequest.form方法的具体用法?Java HttpRequest.form怎么用?Java HttpRequest.form使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.kevinsawicki.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.form方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: post
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
/**
* 构造POST提交表单请求,返回响应结果
* @param params
* 提交的参数
* @param address
* 提交的地址
* @param connectTimeout
* @param readTimeout
* @return
*/
public static HttpResult post(Map<String,String> params,String address,int connectTimeout,int readTimeout) throws IOException {
HttpResult result = new HttpResult();
if(params == null){
params = new HashMap<>();
}
if(StringUtils.isEmpty(address)){
log.error("请求地址不能为空");
return null;
}
URL requestUrl = new URL(address);
long start = System.currentTimeMillis();
HttpRequest httpRequest = new HttpRequest(requestUrl,"POST")
.connectTimeout(connectTimeout).readTimeout(readTimeout).trustAllCerts().trustAllHosts();
httpRequest.form(params,"UTF-8");
result.setStatus(httpRequest.code());
result.setResult(httpRequest.body());
result.setResponseTime(System.currentTimeMillis() - start);
return result;
}
示例2: request
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
/**
* Make request
* @param url String
* @param requestType RequestType
* @param data Map<String, String>
* @param callback Request
*/
private void request(String url, RequestType requestType, Map<String, String> data, final Request callback) {
url = API_URL + url;
String authorization = null;
switch(asanaClient.getConnectionType()) {
case API_KEY:
authorization = "Basic " + Base64.encode(asanaClient.getApiKey().getBytes());
break;
case TOKEN:
authorization = "Bearer " + asanaClient.getToken();
break;
}
HttpRequest httpRequest;
switch(requestType) {
case POST:
httpRequest = HttpRequest.post(url).header("Authorization", authorization);
httpRequest.form(data);
if(httpRequest.ok()) {
callback.onSuccess(httpRequest.body());
}
break;
case GET:
httpRequest = HttpRequest.get(url).header("Authorization", authorization);
if(httpRequest.ok()) {
callback.onSuccess(httpRequest.body());
}
break;
case PUT:
httpRequest = HttpRequest.put(url).header("Authorization", authorization);
httpRequest.form(data);
if(httpRequest.ok()) {
callback.onSuccess(httpRequest.body());
}
break;
}
}
示例3: post
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
/**
* (http) post
* @param address The address of the API
* @param apiKey The username for basic authentication
* @param apiPassword The password for basic authentication
* @param body The request body of the API
* @param requestType The URI part of the API method
* @return The request object
* @throws Exception
*/
public HttpRequest post(String address, String apiKey, String apiPassword, Map body, String requestType) throws Exception {
HttpRequest request = HttpRequest.post(new URL(address));
request.basic(apiKey, apiPassword);
request.accept(String.format("application/%s", requestType));
request.form(body);
return request;
}
示例4: put
import com.github.kevinsawicki.http.HttpRequest; //导入方法依赖的package包/类
/**
* (http) put
* @param address The address of the API
* @param apiKey The username for basic authentication
* @param apiPassword The password for basic authentication
* @param body The request body of the API
* @param requestType The URI part of the API method
* @return The request object
* @throws Exception
*/
public HttpRequest put(String address, String apiKey, String apiPassword, Map body, String requestType) throws Exception {
HttpRequest request = HttpRequest.put(new URL(address));
request.basic(apiKey, apiPassword);
request.accept(String.format("application/%s", requestType));
request.form(body);
return request;
}