當我運行下面的代碼時,我總是收到上述錯誤。所有的跡象都表明了我讀過的COOKIES有問題。如果我是正確的,我將如何去實現CookieManager來解決這個問題?或者,如果這不是COOKIES的問題,我將如何解決問題?服務器返回的HTTP響應代碼:403對於URL :(我該如何解決這個問題?)
public class Client {
public Client(){
}
String executePost(String targetURL, String urlParameters){
URL url;
HttpURLConnection connection = null;
try{
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setChunkedStreamingMode(0);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//send Request
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
dataout.writeBytes(urlParameters);
dataout.flush();
dataout.close();
//get response
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = br.readLine()) != null){
response.append(line);
response.append('\n');
}
System.out.println(response.toString());
br.close();
return response.toString();
}catch(Exception e){
System.out.println("Unable to full create connection");
e.printStackTrace();
return null;
}finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
可能是你不允許訪問該網址... – 2012-03-04 09:41:48
@Tomas我如何解決它?我將信息發送到位於PUBLIC_HTML文件夾中的PHP文件。 PHP文件然後訪問一個mysql數據庫並返回請求的數據。 – Fabii 2012-03-04 09:59:04
@Tomas--表面上看來,這是正確的。但是,它假定服務器正在使用正確的狀態碼。 – 2012-03-04 10:00:56