2013-04-27 72 views
0

伊夫看了好多其他線程在這裏和所有在互聯網上,我似乎無法解決這個..爲什麼我的HTTP POST請求不會發送到我的網頁?

所以基本上我寫了這個Java代碼:

public void sendReport(CommandSender sender, Player target, String reason) 
{ 
    HttpURLConnection connectionStandard = null; 
    String email = config.getString("rp.site.email"); 
    String password = config.getString("rp.site.password"); 
    String senderName = sender.getName(); 
    String targetName = target.getDisplayName(); 
    String reasonString = reason; 

    try 
    { 

     URL url = new URL("http://www.website.net/folder/connect.php");  
     HttpURLConnection request = (HttpURLConnection)url.openConnection(); 
     request.setRequestProperty("Content-type","application/x-www-form-urlencoded"); 
     request.setRequestMethod("POST"); 
     request.setDoOutput(true); 
     OutputStreamWriter post = new OutputStreamWriter(request.getOutputStream()); 
     String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email.toString(), "UTF-8"); 
     data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password.toString(), "UTF-8"); 
     data += "&" + URLEncoder.encode("reporter", "UTF-8") + "=" + URLEncoder.encode(senderName, "UTF-8"); 
     data += "&" + URLEncoder.encode("reported", "UTF-8") + "=" + URLEncoder.encode(targetName, "UTF-8"); 
     data += "&" + URLEncoder.encode("reason", "UTF-8") + "=" + URLEncoder.encode(reasonString, "UTF-8"); 
     post.write(data); 
     post.flush(); 
    } catch (MalformedURLException e) 
    { 
     e.printStackTrace(); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } finally 
    { 
     if(null != connectionStandard) 
     { 
      connectionStandard.disconnect(); 
     } 
    } 
} 


基本上,我的PHP代碼看起來就像這樣:

<?php 

require_once "db.php"; 

$reporter->startSecureSession(); 

foreach ($_POST as $post => $value) { 
    $post = strip_tags($post); 
    $value = strip_tags($value); 
} 

if(!(isset($_POST['email'])) or (!isset($_POST['password'])) or (!isset($_POST['reporter'])) or (!isset($_POST['reported'])) or (!isset($_POST['reason']))) { 
    exit("Invalid Request"); 
} 

$password = hash("sha512", $_POST['password']); 

$reporter->login(array("email" => $_POST['email'], "password" => $password), "plugin"); 
if($reporter->validateClient()) { 

    $reporter->sendReport($_POST); 
    header("Location: logout.php"); 
    exit(); 

} else { 
    exit(); 
} 

?> 

當我通過Chrome傳送我的細節,網頁,它的工作原理,併發送東西到我的數據庫,但是當我做了我的bukkit服務器通過發送請求命令時,它不:/

感謝您的幫助:)

回答

3

您可以使用Apache httpclient 4.x從Java發送GET/POST請求。

String url = "http://www.website.net/folder/connect.php"; 
HttpPost method = new HttpPost(url); 
HttpClient httpClient = new DefaultHttpClient(); 
List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>(); 
formparams.add(new BasicNameValuePair("email", email.toString())); 
formparams.add(new BasicNameValuePair("password", password.toString())); 
formparams.add(new BasicNameValuePair("reporter", senderName)); 
formparams.add(new BasicNameValuePair("reported", targetName)); 
formparams.add(new BasicNameValuePair("reason", reasonString)); 
UrlEncodedFormEntity entity = null; 
try { 
    entity = new UrlEncodedFormEntity(formparams, "UTF-8"); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} 
method.setEntity(entity); 
HttpResponse httpResponse = httpClient.execute(method); 
0

那麼從的URLConnection的的Javadoc:連接對象通過調用創建

1)在URL上使用openConnection方法 。

2)設置參數和一般請求屬性是 操縱。

3)使用連接方法 進行到遠程對象的實際連接。

4)遠程對象變爲可用。可以訪問標題 字段和遠程對象的內容。

您似乎沒有撥打URLConnection#connect

+0

request.connect(); 我該怎麼做呢?和我在哪裏將disconnect(); – madcrazydrumma 2013-04-27 04:01:19

相關問題