4

我需要使用 HttpPost 将包含 % (EX: abc%xyz) 的字符串发送到 web 服务。

我使用了以下逻辑,但在 (%) 的位置出现了 IllegalCharacter 异常。

**updatepeople.php?pilotid=1651&firstname=Nexus&lastname=Google&nickname=abc%xyz**

       final int TIMEOUT_MILLISEC = 20000;
      HttpParams httpParams = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
      HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
      HttpConnectionParams.setTcpNoDelay(httpParams, true);
      HttpClient httpClient = new DefaultHttpClient(httpParams);

       URLEncoder.encode(url, "UTF-8");
       HttpPost httpPost = new HttpPost(url);
  ResponseHandler<String> resHandler = new BasicResponseHandler();
  page = httpClient.execute(httpPost, resHandler);
   return page;
4

1 回答 1

2

当您在查询字符串中传递值时尝试使用此代码,而不是将此作为实体传递。

现在您的网址仅包含页面链接,没有任何参数,它将以updatepeople.php

HttpPost httpPost = new HttpPost(url);

List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("pilotid", "1651"));
param.add(new BasicNameValuePair("firstname", "Nexus"));
param.add(new BasicNameValuePair("lastname", "Google"));
param.add(new BasicNameValuePair("nickname", "abc%xyz"));

httpPost.setEntity(new Unew UrlEncodedFormEntity(param));

现在执行它并继续

于 2013-03-25T10:13:02.643 回答