如何使用 Java 发送帖子?
我正在使用 selenium 和 java 对 Web 应用程序运行一些自动化测试。我的 java 代码的一部分,我想将带有 post 数据电子邮件的 http post 请求发送到 php 页面。
帖子数据应如下所示“emails=username@domain.com”
出于某种原因,我似乎无法让它工作,我觉得代码是正确的,但想也许更多的眼睛看看它,可以帮助我确定问题
它编译并运行,但它似乎没有将发布数据发送到 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", "username@domain.com");
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();
}
}