To do http get request with parameters in Angular, we can make use of params options argument in HttpClient. get() method. Using the params property we can pass parameters to the HTTP get request. Either we can pass HttpParams or an object which contains key value pairs of parameters.
To use this function you just need to create two NameValueCollections holding your parameters and request headers. Show activity on this post. You can also pass value directly via URL. If you want to call method public static void calling(string name){....}
GET requests don't have a request body, so all parameters must appear in the URL or in a header. While the HTTP standard doesn't define a limit for how long URLs or headers can be, mostHTTP clients and servers have a practical limit somewhere between 2 kB and 8 kB.
When the GET request method is used, if a client uses the HTTP protocol on a web server to request a certain resource, the client sends the server certain GET parameters through the requested URL. These parameters are pairs of names and their corresponding values, so-called name-value pairs.
I use a List of NameValuePair and URLEncodedUtils to create the url string I want.
protected String addLocationToUrl(String url){
if(!url.endsWith("?"))
url += "?";
List<NameValuePair> params = new LinkedList<NameValuePair>();
if (lat != 0.0 && lon != 0.0){
params.add(new BasicNameValuePair("lat", String.valueOf(lat)));
params.add(new BasicNameValuePair("lon", String.valueOf(lon)));
}
if (address != null && address.getPostalCode() != null)
params.add(new BasicNameValuePair("postalCode", address.getPostalCode()));
if (address != null && address.getCountryCode() != null)
params.add(new BasicNameValuePair("country",address.getCountryCode()));
params.add(new BasicNameValuePair("user", agent.uniqueId));
String paramString = URLEncodedUtils.format(params, "utf-8");
url += paramString;
return url;
}
To build uri with get parameters, Uri.Builder provides a more effective way.
Uri uri = new Uri.Builder()
.scheme("http")
.authority("foo.com")
.path("someservlet")
.appendQueryParameter("param1", foo)
.appendQueryParameter("param2", bar)
.build();
As of HttpComponents 4.2+
there is a new class URIBuilder, which provides convenient way for generating URIs.
You can use either create URI directly from String URL:
List<NameValuePair> listOfParameters = ...;
URI uri = new URIBuilder("http://example.com:8080/path/to/resource?mandatoryParam=someValue")
.addParameter("firstParam", firstVal)
.addParameter("secondParam", secondVal)
.addParameters(listOfParameters)
.build();
Otherwise, you can specify all parameters explicitly:
URI uri = new URIBuilder()
.setScheme("http")
.setHost("example.com")
.setPort(8080)
.setPath("/path/to/resource")
.addParameter("mandatoryParam", "someValue")
.addParameter("firstParam", firstVal)
.addParameter("secondParam", secondVal)
.addParameters(listOfParameters)
.build();
Once you have created URI
object, then you just simply need to create HttpGet
object and perform it:
//create GET request
HttpGet httpGet = new HttpGet(uri);
//perform request
httpClient.execute(httpGet ...//additional parameters, handle response etc.
The method
setParams()
like
httpget.getParams().setParameter("http.socket.timeout", new Integer(5000));
only adds HttpProtocol parameters.
To execute the httpGet you should append your parameters to the url manually
HttpGet myGet = new HttpGet("http://foo.com/someservlet?param1=foo¶m2=bar");
or use the post request the difference between get and post requests are explained here, if you are interested
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1","value1");
String query = URLEncodedUtils.format(params, "utf-8");
URI url = URIUtils.createURI(scheme, userInfo, authority, port, path, query, fragment); //can be null
HttpGet httpGet = new HttpGet(url);
URI javadoc
Note: url = new URI(...)
is buggy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With