【发布时间】:2012-03-04 09:36:47
【问题描述】:
当我运行下面的代码时,我不断收到上述错误。从我所读到的所有迹象都表明 COOKIES 存在问题。如果我是正确的,我将如何实施 CookieManager 来解决这个问题?或者如果不是 COOKIES 的问题,我该如何解决?
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();
}
}
}
}
【问题讨论】:
-
可能是您不允许访问该网址...
-
@Tomas 我该如何解决?我正在向位于我的 PUBLIC_HTML 文件夹中的 PHP 文件发送信息。然后 PHP 文件访问 mysql 数据库并返回请求的数据。
-
@Tomas - 从表面上看,这是正确的。但是,它假定服务器正在使用正确的状态码。