我需要通过发布请求来验证用户。我正在使用 REST 协议发布我的 xml 数据,但出现 403 错误。请帮助我找出我做错了什么,下面是我要发布的代码。
public void makePostConnection(String fileUrl,
HashMap<String, String> requestData) {
URL url;
HttpURLConnection urlConnection;
try {
url = new URL(fileUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(fileUrl);
httppost.addHeader("accept", "text/xml");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
requestData.size());
System.out.println("Size= " + requestData.size());
for (Entry<String, String> entry : requestData.entrySet()) {
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
System.out.println("Key= " + entry.getKey() + " Value= "
+ entry.getValue());
}
System.out.println("Request ba= " + nameValuePairs.toString());
StringEntity requestBody = new StringEntity(
nameValuePairs.toString());
// requestBody.setContentType("text/xml");
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(requestBody);
// httppost.set
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int code = response.getStatusLine().getStatusCode();
System.out.println("Code= " + code);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}