如何使用Java發送帖子?如何發送帖子?
我正在使用selenium和java對Web應用程序運行一些自動化測試。 我的java代碼的一部分,我想發送一個帶post數據郵件的http post請求到一個php頁面。
後的數據應該是這樣的「[email protected]」
出於某種原因,我似乎無法得到它的工作,我覺得自己的代碼是正確的,但想也許有些更多的眼睛看它,並可以幫助我確定問題
它編譯和運行,但它似乎並沒有將發佈數據發送到php文件,它看起來像。
那麼有沒有辦法輸出它正在發送到PHP文件?
而且,有沒有辦法從服務器返回消息?如「無效的請求」或成功或其他。
而且我不確定它在代碼中缺少什麼,如果我錯過了什麼,只要完成正確的POST。 :
package drtest;
//for selenium each part of the test
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
// classes for selenium to work properly in java
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import com.thoughtworks.selenium.Selenium;
//java .net libraries
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.io.*;
//selenium java integration
public class devicereplication extends SeleneseTestBase {
public Selenium selenium;
@BeforeMethod // this part of the code points to selenium server and launches the web browser
public void beforeMethod() {
selenium = new DefaultSelenium("localhost", 4444, "firefox",
"https://example.com");
selenium.start();
}
@Test // this part of the code is the actual test being run in both java and selenium
public void testme() {
//login steps
selenium.open("/dashboard/login/");
selenium.click("id=email");
selenium.type("id=email", "[email protected]");
selenium.type("id=pass", "strongpassword");
selenium.click("id=submitInfo");
selenium.waitForPageToLoad("30000");
// deploy agent steps
selenium.open("/dashboard/");
selenium.click("link=Deployment");
selenium.click("link=Deploy Agents");
selenium.waitForPageToLoad("90000");
try {
int userCount = 5; // how many automation users in the db you are adding a device to.
int counter = 1;
String emailDomain = "@domain.com";
String userName = "username";
for (;counter <= userCount; counter ++) //this loop adds incremental values to each e-mail/username combo.
{
String combinedEmail = (userName+counter+emailDomain); //turns the email name into a string that can be looped.
String request = "https://examples.com/example/example.php";
URL url = new URL(request); //requests url
String param=URLEncoder.encode(combinedEmail, "UTF-8");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
// connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Host", "example.com");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
// connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
// connection.setRequestProperty("X-Request", "JSON");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
connection.setRequestProperty("Referer", "https://example.com/example/example.php");
// connection.setRequestProperty("Content-Length", "344");
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(param);
wr.flush();
wr.close();
connection.disconnect();
}
} catch(MalformedURLException ex) {
System.out.println(ex.getMessage());
} catch(IOException ex){
System.out.println(ex.getMessage());
}
}
@AfterMethod
public void afterMethod() {
//selenium.stop();
}
}
_I似乎無法得到它的工作_解釋。 –
它編譯並運行,但它似乎並沒有將發佈數據發送到php文件,它看起來像。 那麼有沒有辦法輸出它正在發送到php文件的內容? 而且,有沒有辦法從服務器返回消息? 就像「無效的請求」或成功或什麼的。 我不確定代碼中缺少什麼,如果我缺少任何東西。 – adbarads