本文整理匯總了Java中org.apache.http.client.methods.HttpPost.setURI方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpPost.setURI方法的具體用法?Java HttpPost.setURI怎麽用?Java HttpPost.setURI使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.methods.HttpPost
的用法示例。
在下文中一共展示了HttpPost.setURI方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: postOverrideContentType
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
@Test public void postOverrideContentType() throws Exception {
server.enqueue(new MockResponse());
HttpPost httpPost = new HttpPost();
httpPost.setURI(server.url("/").url().toURI());
httpPost.addHeader("Content-Type", "application/xml");
httpPost.setEntity(new StringEntity("<yo/>"));
client.execute(httpPost);
RecordedRequest request = server.takeRequest();
assertEquals(request.getHeader("Content-Type"), "application/xml");
}
示例2: updateMember
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
/**
* Update a subscription to a group given a blank {@link Subscription}
* object with only the updated
* fields set.
* Example:
*
* <pre>
* final Subscription subToUpdate = new Subscription();
* subToUpdate.setAutoFollowReplies(true);
* final Subscription updateSub = client.member().updateMember(subToUpdate);
* </pre>
*
* @param subscription
* - with only the updated fields set
* @return the full {@link Subscription} after a successful update
* @throws URISyntaxException
* @throws IOException
* @throws GroupsIOApiException
*/
public Subscription updateMember(final Subscription subscription) throws URISyntaxException, IOException, GroupsIOApiException
{
if (apiClient.group().getPermissions(subscription.getGroupId()).getManageMemberSubscriptionOptions())
{
final URIBuilder uri = new URIBuilder().setPath(baseUrl + "updatemember");
final HttpPost request = new HttpPost();
final Map<String, Object> map = OM.convertValue(subscription, Map.class);
final List<BasicNameValuePair> postParameters = new ArrayList<>();
for (final Entry<String, Object> entry : map.entrySet())
{
postParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
request.setEntity(new UrlEncodedFormEntity(postParameters));
request.setURI(uri.build());
return callApi(request, Subscription.class);
}
else
{
final Error error = new Error();
error.setType(GroupsIOApiExceptionType.INADEQUATE_PERMISSIONS);
throw new GroupsIOApiException(error);
}
}
示例3: updateGroup
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
/**
* Update a group given a blank {@link Group} object with only the updated
* fields set.
* Example:
*
* <pre>
* final Group groupToUpdate = new Group();
* groupToUpdate.setWebsite("https://github.com/lake54/groupsio-api-java");
* final Group updatedGroup = client.group().updateGroup(groupToUpdate);
* </pre>
*
* @param group
* - with only the updated fields set
* @return the full {@link Group} after a successful update
* @throws URISyntaxException
* @throws IOException
* @throws GroupsIOApiException
*/
public Group updateGroup(final Group group) throws URISyntaxException, IOException, GroupsIOApiException
{
if (apiClient.group().getPermissions(group.getId()).getManageGroupSettings())
{
final URIBuilder uri = new URIBuilder().setPath(baseUrl + "updategroup");
final HttpPost request = new HttpPost();
final Map<String, Object> map = OM.convertValue(group, Map.class);
final List<BasicNameValuePair> postParameters = new ArrayList<>();
for (final Entry<String, Object> entry : map.entrySet())
{
postParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
request.setEntity(new UrlEncodedFormEntity(postParameters));
request.setURI(uri.build());
return callApi(request, Group.class);
}
else
{
final Error error = new Error();
error.setType(GroupsIOApiExceptionType.INADEQUATE_PERMISSIONS);
throw new GroupsIOApiException(error);
}
}
示例4: getPostResponse
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
private HttpResponse getPostResponse(String apiUrl, HttpPost httpPost) throws IOException {
try{
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
String[] urlAndParame = apiUrl.split("\\?");
String apiUrlNoParame = urlAndParame[0];
Map<String, String> parameMap = StrUtil.splitUrlToParameMap(apiUrl);
nvps = paramsConverter(parameMap);
httpPost.setURI(new URI(apiUrlNoParame));
if(reqHeader != null) {
Iterator<String> iterator = reqHeader.keySet().iterator();
while(iterator.hasNext()) {
String key = iterator.next();
httpPost.addHeader(key, reqHeader.get(key));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, postDataEncode));
return httpClient.execute(httpPost);
}catch(Exception e){
throw new IllegalStateException(e);
}
}
示例5: responseContentUri
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
public static String responseContentUri(URI uri) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost();
request.setURI(uri);
InputStream is = client.execute(request).getEntity().getContent();
BufferedReader inb = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder("");
String line;
String NL = System.getProperty("line.separator");
while ((line = inb.readLine()) != null) {
sb.append(line).append(NL);
}
inb.close();
return sb.toString();
}
示例6: uploadFile
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
@Override
public String uploadFile(String systemName, String entityName, String apiUrl, List<File> fileList, String requestParamInputName) throws IllegalStateException {
HttpResponse response = null;
HttpPost httpPost = getHttpPost();
try {
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
String[] urlAndParame = apiUrl.split("\\?");
String apiUrlNoParame = urlAndParame[0];
Map<String, String> parameMap = StrUtil.splitUrlToParameMap(apiUrl);
Iterator<String> parameIterator = parameMap.keySet().iterator();
httpPost.setURI(new URI(apiUrlNoParame));
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
while (parameIterator.hasNext()) {
String parameName = parameIterator.next();
nvps.add(new BasicNameValuePair(parameName, parameMap.get(parameName)));
multipartEntity.addPart(parameName, new StringBody(parameMap.get(parameName), ContentType.create("text/plain", Consts.UTF_8)));
}
if (!StrUtil.isBlank(systemName)) {
multipartEntity.addPart("systemName", new StringBody(systemName, ContentType.create("text/plain", Consts.UTF_8)));
}
if (!StrUtil.isBlank(entityName)) {
multipartEntity.addPart("entityName", new StringBody(entityName, ContentType.create("text/plain", Consts.UTF_8)));
}
// 多文件上傳 獲取文件數組前台標簽name值
if (!StrUtil.isBlank(requestParamInputName)) {
multipartEntity.addPart("filesName", new StringBody(requestParamInputName, ContentType.create("text/plain", Consts.UTF_8)));
}
if (fileList != null) {
for (int i = 0, size = fileList.size(); i < size; i++) {
File file = fileList.get(i);
multipartEntity.addBinaryBody(requestParamInputName, file, ContentType.DEFAULT_BINARY, file.getName());
}
}
HttpEntity entity = multipartEntity.build();
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
String statusCode = String.valueOf(response.getStatusLine().getStatusCode());
if (statusCode.indexOf("20") == 0) {
entity = response.getEntity();
// String contentType = entity.getContentType().getValue();//
// application/json;charset=ISO-8859-1
return StrUtil.readStream(entity.getContent(), responseContextEncode);
} else {
LOG.error("返回狀態碼:[" + statusCode + "]");
return "返回狀態碼:[" + statusCode + "]";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return null;
}
示例7: updateUser
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
/**
* Update a user given a blank {@link User} object with only the updated
* fields set.
* Example:
*
* <pre>
* final User userToUpdate = new User();
* userToUpdate.setPerPagePref("user_per_page_pref50");
* final User updatedUser = client.user().updateUser(userToUpdate);
* </pre>
*
* @param user
* - with only the updated fields set
* @return the full {@link User} after a successful update
* @throws URISyntaxException
* @throws IOException
* @throws GroupsIOApiException
*/
public User updateUser(final User user) throws URISyntaxException, IOException, GroupsIOApiException
{
final URIBuilder uri = new URIBuilder().setPath(baseUrl + "updateuser");
final HttpPost request = new HttpPost();
final Map<String, Object> map = OM.convertValue(user, Map.class);
final List<BasicNameValuePair> postParameters = new ArrayList<>();
for (final Entry<String, Object> entry : map.entrySet())
{
postParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
request.setEntity(new UrlEncodedFormEntity(postParameters));
request.setURI(uri.build());
return callApi(request, User.class);
}
示例8: uploadFileByByte
import org.apache.http.client.methods.HttpPost; //導入方法依賴的package包/類
@Override
public String uploadFileByByte(String fileUrl, byte[] fileByte, String requestParamInputName) throws IllegalStateException {
HttpResponse response = null;
HttpPost httpPost = getHttpPost();
try {
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
String[] urlAndParame = fileUrl.split("\\?");
String apiUrlNoParame = urlAndParame[0];
Map<String, String> parameMap = StrUtil.splitUrlToParameMap(fileUrl);
Iterator<String> parameIterator = parameMap.keySet().iterator();
httpPost.setURI(new URI(apiUrlNoParame));
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
while (parameIterator.hasNext()) {
String parameName = parameIterator.next();
nvps.add(new BasicNameValuePair(parameName, parameMap.get(parameName)));
multipartEntity.addPart(parameName, new StringBody(parameMap.get(parameName), ContentType.create("text/plain", Consts.UTF_8)));
}
// 多文件上傳 獲取文件數組前台標簽name值
if (!StrUtil.isBlank(requestParamInputName)) {
multipartEntity.addPart("filesName", new StringBody(requestParamInputName, ContentType.create("text/plain", Consts.UTF_8)));
}
multipartEntity.addBinaryBody(requestParamInputName, fileByte);
HttpEntity entity = multipartEntity.build();
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
String statusCode = String.valueOf(response.getStatusLine().getStatusCode());
if (statusCode.indexOf("20") == 0) {
entity = response.getEntity();
// String contentType = entity.getContentType().getValue();//
// application/json;charset=ISO-8859-1
return StrUtil.readStream(entity.getContent(), responseContextEncode);
} else {
LOG.error("返回狀態碼:[" + statusCode + "]");
return "返回狀態碼:[" + statusCode + "]";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return null;
}