get Url By Post
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.util.Log;
class Main {
private static HttpParams defaultHttpParams = new BasicHttpParams();
private static final String UTF8 = "UTF-8";
public static String getUrlByPost(String url, Map<String, String> params, Map<String, String> headers,
int maxRetries) throws IOException {
String result = null;
int retries = 0;
DefaultHttpClient httpclient = new DefaultHttpClient(defaultHttpParams);
httpclient.setCookieStore(null);
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
if (params != null) {
Set<Entry<String, String>> paramsSet = params.entrySet();
for (Entry<String, String> entry : paramsSet) {
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formParams, UTF8);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(postEntity);
if (headers != null) {
Set<Entry<String, String>> headersSet = headers.entrySet();
for (Entry<String, String> entry : headersSet) {
httppost.setHeader(entry.getKey(), entry.getValue());
}
}
while (retries < maxRetries && result == null) {
try {
retries++;
HttpEntity responseEntity = httpclient.execute(httppost).getEntity();
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity).trim();
}
} catch (SocketException se) {
if (retries > maxRetries) {
throw se;
} else {
// Log.v(TAG, "SocketException, retrying " + retries, se);
}
}
}
return result;
}
}
Related examples in the same category