I'm executing the following code:
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("(...)&avatarUrl={(...)}&socialId=1&sexo=m&username="));
My problem is I'm getting an illegalArgumentException at letter l from the word avatarUrl, and I don't understand why.
I'll really appreciate your help.
Besides the question of ? oder & in your code, there are cleaner ways to pass parameters:
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
BasicHttpParams params = new BasicHttpParams();
params.setParameter("avatarUrl","...")
.setParameter("socialId","...")
.setParameter("sexo","...")
.setParameter("username","...");
request.setParams(params);
HttpResponse response = httpclient.execute(request);
(not tested, but should work)
Note: I heavily edited my original answer due to a little misunderstanding.
Looking at the source code of HttpGet, this exception means that the URL is invalid:
The first query string parameter should be preceded with ?
{ and } are not valid characters for URLs and should be escaped
Related
Hi am sending a url using apache HttpClient by using following code but it has been showing a exception :java.net.URISyntaxException:
Illegal character in query at index 70: http://192.155.2.144:8080/SDAX/homePage.do?actionFlag=istrict&&MSG=1|Bdrtfggf|254td|return|null|null|null
Please help me where iam doing the problem. the following code i am sending a URL
String MSG="1|Bdrtfggf|254td|return|null|null|null" ;
String url="http://192.168.2.144:8080/SDAX/homePage.do?actionFlag=edistrict&&MSG="+MSG;
System.out.println("Url is"+url);
//String url = "http://192.168.0.6:8084/NRC_NEW_SEARCH/getVillageList.req?dist_id=1";
//String url="http://192.168.0.85:8080/poly/web/";
//FacesContext.getCurrentInstance().getExternalContext().redirect(url);
//ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//context..redirect(url);
HttpRequestBase request = new HttpGet(url);
/*HttpParams params = new BasicHttpParams();
params.setParameter("dist_id", "1");
request.setParams(params);*/
HttpClient httpClient = new DefaultHttpClient();
httpClient.execute(request);
You should encode the MSG string before creating a URL from it.
String encodedMSG = URLEncoder.encode(MSG, "UTF-8")
String url="http://192.168.2.144:8080/SDAX/homePage.do?actionFlag=edistrict&&MSG="+ encodedMSG;
Edit
There won't be any problem in retrieving the data after encoding. If you have programmed this servlet homePage.do then you should use URLDecoder.decode() method in it.
Since vertical bar(|) is not a valid URI character thats why You are getting URISyntaxException.
Solution:
As suggested by kaysush, you need to encode/decode your url.
For more on this, Please check folowing url:
Cannot process url with vertical/pipe bar
You havn't explained what you are trying to achieve here. I hope it isn't by mistake. but
As per you question, You are trying to post a url and In your code you are using HttpGet(url);
Hi I'm trying to send an ArrayList as a parameter for a POST request from my Android app to a server. So far I have this code:
HttpResponse response;
List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
postParams.add(new BasicNameValuePair("post[text]", text));
postParams.add(new BasicNameValuePair("post[common_comments]", String.valueOf(commonComments));
postParams.add(new BasicNameValuePair("post[wall_ids]", wallIds);
UrlEncodedFormEntity encodedParams;
try {
encodedParams = new UrlEncodedFormEntity(postParams);
post.setEntity(encodedParams);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
but BasicNameValuePair only receives String as value. Is there any way I can send an ArrayList as a value for post[wall_ids]?
Thanks in advance.
Bueno días Carla.
I don´t know if you had resolve this (two months is a lot of time)... but maybe it can help you:
for(String wallyID: wallyIDs)
postParams.add(new BasicNameValuePair("extraFields[wallyIDs][]", wallyID));
It would also be good to use the "utf 8" format, if you want to use latin characters (this will make to forget on the server), something like:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
I have a question about the List constructor... why do you use 2 as argument for it??
PS: I would like to make a coment and not an answer, because I don´t know if it could work as you want.
I'm trying to send the equivalent of the curl '-F' option to a designated URL.
This is what the command looks like using Curl:
curl -F"optionName=cool" -F"file=#myFile" http://myurl.com
I believe I am correct in using the HttpPost class in the Apache httpcomponents library.
I supply a name=value type of parameter. The optionName is simply a string and the 'file' is a file I have stored locally on my drive (hence the #myFile to indicate its a local file).
If I print the response I get an HTTP 500 error... I am not sure what is causing the issue here because the server responds as it should when using the Curl command mentioned above. Is there some simple mistake I am making when looking at the code below?
HttpPost post = new HttpPost(postUrl);
HttpClient httpClient = HttpClientBuilder.create().build();
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair(optionName, "cool"));
nvps.add(new BasicNameValuePair(file, "#myfile"));
try {
post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse response = httpClient.execute(post);
// do something with response
} catch (Exception e) {
e.printStackTrace();
}
Try to use a MultipartEntity instead of an UrlEncodedFormentity, to handle both parameters and file upload:
MultipartEntity entity = new MultipartEntity();
entity.addPart("optionName", "cool");
entity.addPart("file", new FileBody("/path/to/your/file"));
....
post.setEntity(entity);
Edit
MultipartEntity is deprecated and FileBody constructor takes a File, not a String, so:
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addTextBody("optionName", "cool");
entity.addPart("file", new FileBody(new File("/path/to/your/file")));
....
post.setEntity(entity.build());
Thanks #CODEBLACK .
When I am using SoapUI to call this web service I am getting the correct response but when I implement this in android, I am getting the below exception,
system.web.services.protocols.soapheaderexception (some information is missing).
This is what I tried,
HttpPost httppost = new HttpPost("http://www.ocrwebservice.com/services/OCRWebService.asmx");
StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httppost.setEntity(se);
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse =
(BasicHttpResponse) httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
I tried other combinations also like,
httppost.setHeader("Accept-Charset","utf-8")
and
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8")
But nothing worked.
The error says,
System.Web.Services.Protocols.SoapHeaderException: WSE012: The input was not a valid SOAP message because the following information is missing: action.
System.Web.Services.Protocols.SoapHeaderException: WSE012: The input
was not a valid SOAP message because the following information is
missing: action.
=> As per the above exception, I can say you forgot to set Action.
Try:
String SOAP_ACTION = "http://stockservice.contoso.com/wse/samples/2005/10/OCRWebServiceAvailablePages";
httppost.setHeader("SOAPAction", SOAP_ACTION);
The code works fine if the query does not contain any utf-8 chars. As soon as there is one utf-8 char then ETools provides results I do not expect. For example for "trees" I get correct result and for "bäume" (german word for trees) I get strange results. It looks like that ETools receives the query as "b%C3%A4ume" and looks for exact that query with exact those chars and not for "bäume". I think the problem may be solved if I set some header parameters but I dont know what parameters are possible there.
String query = "some+query+with+utf8+chars";
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost();
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("query", query));
parameters.add(new BasicNameValuePair("country", "web"));
parameters.add(new BasicNameValuePair("language", "all"));
parameters.add(new BasicNameValuePair("dataSourceResults", String.valueOf(40)));
parameters.add(new BasicNameValuePair("pageResults", String.valueOf(40)));
request.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setURI("http://www.etools.ch/searchAdvancedSubmit.do?page=2");
MyResponse myResponse = client.execute(request, myResponseHandler);
request.reset();
client.getConnectionManager().shutdown();
You should add your charset into the Content-Type at least (the default is latin1):
request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
If that doesn't work, it could be a server bug. You may want to try submitting the form as multipart/form-data (RFC 2388) instead of URL encoded. There is already a StackOverflow answer with an example that you can use.