-2

Javaを使用して投稿を送信するには?

Selenium と Java を使用して、Web アプリケーションに対していくつかの自動テストを実行しています。私のJavaコードの一部で、投稿データの電子メールを含むhttp投稿リクエストを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();
       }

}
4

2 に答える 2