use of org.apache.http.client.methods.HttpPost in project OpenRefine by OpenRefine.
the class RefineBroker method getUserId.
// ----------------------------------------------------------------------------------------
@SuppressWarnings("unchecked")
protected String getUserId(HttpServletRequest request) throws Exception {
// This is useful for testing
if (developmentMode) {
return getParameter(request, "uid");
}
String oauth = request.getHeader(DELEGATED_OAUTH_HEADER);
if (oauth == null) {
throw new RuntimeException("The request needs to contain the '" + DELEGATED_OAUTH_HEADER + "' header set to obtain user identity via Freebase.");
}
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
Map<String, String> params = (Map<String, String>) request.getParameterMap();
for (Entry<String, String> e : params.entrySet()) {
formparams.add(new BasicNameValuePair((String) e.getKey(), (String) e.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httpRequest = new HttpPost(USER_INFO_URL);
httpRequest.setHeader(OAUTH_HEADER, oauth);
httpRequest.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenRefine Broker");
httpRequest.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpRequest, responseHandler);
JSONObject o = new JSONObject(responseBody);
return o.getString("username");
}
use of org.apache.http.client.methods.HttpPost in project screenbird by adamhub.
the class FileUtil method postData.
public static String[] postData(String data, final JProgressBar pbUpload, String anonymous_token, String csrf_token, String user_id, ProgressBarUploadProgressListener listener, String upload_url) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(upload_url);
//File file = new File(fileLocation);
//MultipartEntity mpEntity = new MultipartEntity();
ProgressMultiPartEntity mpEntity = new ProgressMultiPartEntity(listener);
//ContentBody cbFile = new FileBody(file, "video/quicktime");
//mpEntity.addPart("videoupload", cbFile);
//ContentBody cbToken = new StringBody(title);
//mpEntity.addPart("csrfmiddlewaretoken", cbToken);
ContentBody cbUser_id = new StringBody(user_id);
mpEntity.addPart("user_id", cbUser_id);
ContentBody cbAnonym_id = new StringBody(MediaUtil.getMacAddress());
mpEntity.addPart("anonym_id", cbAnonym_id);
//ContentBody cbTitle = new StringBody(title);
//mpEntity.addPart("title", cbTitle);
// ContentBody cbSlug = new StringBody(slug);
// mpEntity.addPart("slug", cbSlug);
// ContentBody cbPublic = new StringBody(is_public ? "1" : "0");
// mpEntity.addPart("is_public", cbPublic);
// ContentBody cbDescription = new StringBody(description);
// mpEntity.addPart("description", cbDescription);
// ContentBody cbChecksum = new StringBody(checksum);
// mpEntity.addPart("checksum", cbChecksum);
// ContentBody cbType = new StringBody(video_type);
// mpEntity.addPart("video_type", cbType);
httppost.setEntity(mpEntity);
System.out.println("===================================================");
System.out.println("executing request " + httppost.getRequestLine());
System.out.println("===================================================");
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
String[] result = new String[2];
String status = response.getStatusLine().toString();
result[0] = (status.indexOf("200") >= 0) ? "200" : status;
System.out.println("===================================================");
System.out.println("status from request " + result[0]);
System.out.println("===================================================");
if (resEntity != null) {
result[1] = EntityUtils.toString(resEntity);
EntityUtils.consume(resEntity);
}
httpclient.getConnectionManager().shutdown();
return result;
}
use of org.apache.http.client.methods.HttpPost in project FastDev4Android by jiangqqlmj.
the class IoUtils method sendMessageByPost.
/**
* post投递
*/
public static void sendMessageByPost(String url, HashMap<String, String> map) {
if (url == null || url.equals("")) {
return;
}
Log.d(TAG_LISTLOGIC, "post投递地址:" + url);
HttpPost httpPost = null;
URI encodedUri = getEncodingURI(url);
httpPost = new HttpPost(encodedUri);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DELIVER_CONN_TIMEOUT);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DELIVER_SOCKET_TIMEOUT);
try {
if (map != null) {
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey().toString();
String value = null;
if (entry.getValue() == null) {
value = "";
} else {
value = entry.getValue().toString();
}
Log.d(TAG_LISTLOGIC, "post投递参数" + key + "=" + value);
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, value);
nameValuePair.add(basicNameValuePair);
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));
}
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse != null) {
int code = httpResponse.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK) {
Log.d(TAG_LISTLOGIC, "post投递服务器返回200");
return;
} else {
httpPost.abort();
}
} else {
httpPost.abort();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
httpClient = null;
}
}
}
use of org.apache.http.client.methods.HttpPost in project FastDev4Android by jiangqqlmj.
the class HttpClientStackTest method createPostRequestWithBody.
@Test
public void createPostRequestWithBody() throws Exception {
TestRequest.PostWithBody request = new TestRequest.PostWithBody();
assertEquals(request.getMethod(), Method.POST);
HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
assertTrue(httpRequest instanceof HttpPost);
}
use of org.apache.http.client.methods.HttpPost in project Klyph by jonathangerbaud.
the class HttpRequest2 method send.
public String send() {
client = AndroidHttpClient.newInstance("Android");
HttpPost request = new HttpPost(url);
if (params != null) {
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
request.setEntity(entity);
} catch (UnsupportedEncodingException e) {
Log.e("", "UnsupportedEncodingException: " + e);
}
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = "";
try {
response = client.execute(request, responseHandler);
} catch (ClientProtocolException e) {
Log.e("HttpRequest2", e.toString());
} catch (IOException e) {
Log.e("HttpRequest2", e.toString());
}
if (LOG_RESPONSE == true)
Log.i("HttpRequest2", response);
if (HTML_TRANSFORM == true)
response = Html.fromHtml(response).toString();
close();
return response;
}
Aggregations