简体   繁体   中英

HTTP Post request with URL as parameter

I need send https request to server using parameters one of them is URL: I do next:

HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(APIURL);
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("url", "https://api/v1/pictureadress/id"));
 ...

And I got error when add URL parameter. But if I add other parameters, except url, like age, gender etc. I have no errors. WHat I do wrong?

I have no exposure to apache component libraries. But looking at the api on BasicNameValuePair , I understand that the input name and values are stored raw and not encoded . I fear that is the reason why your url parameter's value threw an error. You may require UrlEncodedFormEntity unless already using to handle url encoded name value pairs.

Try this..

public void postData() throws Exception {


 HttpClient client = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.xyz.com");

 List<NameValuePair> list = new ArrayList<NameValuePair>(1);

 list.add(new BasicNameValuePair("name","ABC");

 httppost.setEntity(new UrlEncodedFormEntity(list));

 HttpResponse r = client.execute(httppost);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM