okhttp3. get请求&post请求
package testng.com;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.json.Json;
import com.google.common.net.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
public class postDemo {
public static void main(String[] args) throws IOException {
String url = "http://132.232.46.158:8080/LKLDemo/demo/CreditResult_LKL.do";
String url1 = "http://132.232.46.158:8080/LKLDemo/demo/CreditResult_LKL.do";
/*
* Map map = new HashMap(); map.put("key", "value"); Json Json = new Json();
*
* RequestBody requestBody =
* RequestBody.create(MediaType.parse("application/json; charset=utf-8"),"")
*/
//使用json方式传参"application/json; charset=utf-8"
OkHttpClient client = new OkHttpClient();
//MediaType type = MediaType.parse("application/json; charset=utf-8");
okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType,
"{\t\r\n\t\"appId\": \"UATD32e92dc52f11498ca134636fdea39909\",\r\n \"userId\": \"317557826419249\",\r\n\t\"creditNo\": \"asdfsd49566e\"\r\n}");
RequestBody body1 = RequestBody.create(mediaType, "{\\r\\n \\\"registerNo\\\": \\\"9000001113153\\\",\\r\\n \\\"appId\\\":\\\"UATD32e92dc52f11498ca134636fdea39909\\\"\\r\\n}");
Request request = new Request.Builder()
.url(url)
.addHeader("Contect-Type", "application/json")
.post(body)
.build();
//使用client发送请求,返回一个响应码
Response response = client.newCall(request).execute();
System.out.println(response.code());
System.out.println(response.headers());
System.out.println(response.body());
}
}
pom.xml配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>te.com</groupId>
<artifactId>tes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>
openfirefox()
package tes.com;
import java.awt.Dimension;
import java.lang.StackWalker.Option;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import okio.Options;
public class openFireFox {
public static void openFireFox() {
//路径必须设置
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
/* ChromeDriver chromeDriver = new ChromeDriver(); */
FirefoxDriver frFirefoxDriver = new FirefoxDriver();
frFirefoxDriver.get("http://www.baidu.com");
frFirefoxDriver.findElement(By.id("kw")).sendKeys("腾讯课堂");
;
frFirefoxDriver.findElement(By.id("su")).click();
// frFirefoxDriver.findElement(By.cssSelector(".bg.s_btn")).click();
//硬性等待
/*
* try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace();
* }
*/
// 隐式等待 在driver实例化完成后设置实例等待,设置超时时间为5秒。全局,只需要设置一次。
//frFirefoxDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//显式等待 用来等待某个条件满足后再继续执行后续代码(如找到元素、元素可以点击,元素已显示)
WebDriverWait webDriverWait = new WebDriverWait(frFirefoxDriver,5);
//元素可见之后执行下一步
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("腾讯课堂-综合性在线终身学习平台")));
frFirefoxDriver.findElement(By.linkText("腾讯课堂-综合性在线终身学习平台")).click();
// frFirefoxDriver.findElement(By.cssSelector()).click();
// System.out.println("当前的url为:"+ frFirefoxDriver.getCurrentUrl());
// System.out.println("当前的url为:"+ frFirefoxDriver.getTitle());
// System.out.println("新窗口的所有句柄:"+ frFirefoxDriver.getWindowHandles());
//org.openqa.selenium.WebDriver.Options options = frFirefoxDriver.manage();
// options.window().fullscreen();
// org.openqa.selenium.Dimension dimension = options.window().getSize();
// System.out.println("窗口的高:"+ dimension.getHeight());
// System.out.println("窗口的x:"+ options.window().getPosition().getX());
/*
* Navigation navigation = frFirefoxDriver.navigate();
*/
/*
* try{ Thread.sleep(2000); }catch(InterruptedException e){ e.printStackTrace();
* }
*/
/*
* navigation.to("http://www.jd.com");
*
* navigation.refresh();
*
* try{ Thread.sleep(2000); }catch(InterruptedException e){ e.printStackTrace();
* } navigation.back();
*/
// frFirefoxDriver.close();
}
public static void outPr() {
FirefoxDriver frFirefox = new FirefoxDriver();
System.out.println("当前的url为:" + frFirefox.getCurrentUrl());
}
}