下面是一些代碼,我用我自己的小程序所使用,對值(通過POST)發送到PHP腳本我的服務器:
我會用這樣的:
String content = "";
content = content + "a=update&gid=" + gid + "&map=" + getMapString();
content = content + "&left_to_deploy=" + leftToDeploy + "&playerColor=" + playerColor;
content = content + "&uid=" + uid + "&player_won=" + didWin;
content = content + "&last_action=" + lastActionCode + "&appletID=" + appletID;
String result = "";
try {
result = requestFromDB(content);
System.out.println("Sending - " + content);
} catch (Exception e) {
status = e.toString();
}
正如你所看到的,我加入了我所有的值發送到「內容」的字符串,然後再調用我requestFromDB方法(這帖子我的「請求」值,並返回服務器的響應):
public String requestFromDB(String request) throws Exception
{
// This will accept a formatted request string, send it to the
// PHP script, then collect the response and return it as a String.
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
// URL of CGI-Bin script.
url = new URL ("http://" + siteRoot + "/globalconquest/applet-update.php");
// URL connection channel.
urlConn = url.openConnection();
// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput (true);
// Let the RTS know that we want to do output.
urlConn.setDoOutput (true);
// No caching, we want the real thing.
urlConn.setUseCaches (false);
// Specify the content type.
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream());
printout.writeBytes (request);
printout.flush();
printout.close();
// Get response data.
input = new DataInputStream (urlConn.getInputStream());
String str;
String a = "";
while (null != ((str = input.readLine())))
{
a = a + str;
}
input.close();
System.out.println("Got " + a);
if (a.trim().equals("1")) {
// Error!
mode = "error";
}
return a;
} // requestFromDB
在我的PHP腳本,我只需要看看$ _ POST我的價值觀。然後我會打印一個回覆。
注意!出於安全原因,您的PHP腳本必須與applet位於同一臺服務器上,否則這將不起作用。
@ luk2302你是完全錯誤的。這是POST請求的工作方式。 – BackSlash
艾姆,那......可悲......真的,早上太早了 - 我會刪除我的評論。 – luk2302