use of org.apache.http.message.BasicNameValuePair in project PneumaticCraft by MineMaarten.
the class PastebinHandler method loginInternal.
public boolean loginInternal(String userName, String password) {
HttpPost httppost = new HttpPost("http://pastebin.com/api/api_login.php");
List<NameValuePair> params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("api_dev_key", DEV_KEY));
params.add(new BasicNameValuePair("api_user_name", userName));
params.add(new BasicNameValuePair("api_user_password", password));
try {
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
userKey = IOUtils.toString(instream, "UTF-8");
if (userKey.startsWith("Bad API request")) {
Log.warning("User tried to log in into pastebin, it responded with the following: " + userKey);
userKey = null;
return false;
}
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
use of org.apache.http.message.BasicNameValuePair in project 9GAG by Mixiaoxiao.
the class MxxHttpUtil method doHttpPost.
/**
* Op Http post request , "404error" response if failed
*
* @param url
* @param map
* Values to request
* @return
*/
public static String doHttpPost(String url, HashMap<String, String> map) {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
HttpPost post = new HttpPost(url);
post.setHeaders(headers);
String result = "ERROR";
ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
if (map != null) {
for (Map.Entry<String, String> entry : map.entrySet()) {
BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
pairList.add(pair);
}
}
try {
HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
result = EntityUtils.toString(response.getEntity(), "UTF-8") + response.getStatusLine().getStatusCode() + "ERROR";
}
} catch (ConnectTimeoutException e) {
result = "TIMEOUTERROR";
} catch (Exception e) {
result = "OTHERERROR";
e.printStackTrace();
}
return result;
}
use of org.apache.http.message.BasicNameValuePair in project nanohttpd by NanoHttpd.
the class GetAndPostIntegrationTest method testPostRequestWithFormEncodedParameters.
@Test
public void testPostRequestWithFormEncodedParameters() throws Exception {
this.testServer.response = "testPostRequestWithFormEncodedParameters";
HttpPost httppost = new HttpPost("http://localhost:8192/");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("age", "120"));
postParameters.add(new BasicNameValuePair("gender", "Male"));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = this.httpclient.execute(httppost, responseHandler);
assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody);
}
use of org.apache.http.message.BasicNameValuePair 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.message.BasicNameValuePair 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;
}
}
}
Aggregations