我正在尝试使用scala和Dispatch库进行HTTPS发布.我找不到将我的连接标记为https而不是http的位置.这是我到目前为止的代码
println("Running Test")
val http = new Http
val req = :/("www.example.com" , 443) / "full/path.asp"
var response: NodeSeq = Text("")
http(req << "username=x&password=y" <> {response = _ } )
response
println("Done Running Test")
Run Code Online (Sandbox Code Playgroud)
编辑
因此,在尝试解决这个问题之后,我追溯了http线需要看起来像什么
http(req.secure << "username=x&password=y" <> {response = _ } )
Run Code Online (Sandbox Code Playgroud)
另外在这个特定的例子中,我需要POST作为application/x-www-form-urlencoded,要求行看起来像这样
http(req.secure << ("username=x&password=y","application/x-www-form-urlencoded") <> {response = _ } )
Run Code Online (Sandbox Code Playgroud)
这将取代40行C++ + Boost + Asio代码.
我有一些处理HTTP请求的代码,我想对它进行单元测试.
因此,我正在尝试模拟dispatch.Http甚至更好的dispatch.HttpExecutor(0.8.5)与Scala(2.9.1.final),Mockito(1.9.0-rc1)和ScalaTest(1.6.1),但即使可以'让我的测试代码可编辑.
在MyHttpTest中,我希望收到任何HTTP请求的某些HTTP响应:
import org.scalatest.FunSuite
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito.when
import org.mockito.Matchers.any
import dispatch._
class MyHttpTest extends FunSuite with MockitoSugar {
test("example") {
val httpMock = mock[HttpExecutor]
when(httpMock.apply(any(classOf[Handler[String]]))).thenReturn("Some_HTTP_response")
}
}
Run Code Online (Sandbox Code Playgroud)
但它会产生编译错误:
error: overloaded method value thenReturn with alternatives:
(httpMock.HttpPackage[String],<repeated...>[httpMock.HttpPackage[String]])org.mockito.stubbing.OngoingStubbing[httpMock.HttpPackage[String]] <and>
(httpMock.HttpPackage[String])org.mockito.stubbing.OngoingStubbing[httpMock.HttpPackage[String]]
cannot be applied to (java.lang.String)
when(httpMock.apply(any(classOf[Handler[String]]))).thenReturn("Some_response")
Run Code Online (Sandbox Code Playgroud)
那么如何模拟调度客户端呢?
我需要能够发送证书文件(.pem,我认为),使用scala和dispatch发送get请求.
你是怎样做的?
哪里来的电话到dispatch.Http.shutdown()是否有n独立的HTTP调用等,例如:
import com.typesafe.scalalogging.slf4j.Logging
import org.json4s._
import org.json4s.native.JsonMethods._
import scala.util.{ Failure, Success }
object Main extends App with Logging {
logger.debug("github.cli")
// GET /users/defunkt: `curl https://api.github.com/users/defunkt`
val host: dispatch.Req = dispatch.host("api.github.com").secure
val request: dispatch.Req = host / "users" / "defunkt"
logger.debug(s"Request URL: ${request.url}")
import dispatch.Defaults.executor
dispatch.Http(request > dispatch.as.Response(_.getHeaders())) onComplete {
case Success(h) => logger.debug(h.toString())
case Failure(e) => logger.debug(s"Error: $e")
}
dispatch.Http(request OK dispatch.as.json4s.Json) onComplete {
case Success(j) => logger.debug(j.toString())
case Failure(e) => logger.debug(s"Error: $e")
}
//dispatch.Http.shutdown() // …Run Code Online (Sandbox Code Playgroud) 我很困惑如何在调度和提升中组合json库来解析我的json响应.
我显然是斯卡拉新手.
我写了这段代码:
val status = {
val httpPackage = http(Status(screenName).timeline)
val json1 = httpPackage
json1
}
Run Code Online (Sandbox Code Playgroud)
现在我被困在如何解析twitter json响应
我试过使用JsonParser:
val status1 = JsonParser.parse(status)
Run Code Online (Sandbox Code Playgroud)
但得到了这个错误:
<console>:38: error: overloaded method value parse with alternatives:
(s: java.io.Reader)net.liftweb.json.JsonAST.JValue<and>
(s: String)net.liftweb.json.JsonAST.JValue
cannot be applied to (http.HttpPackage[List[dispatch.json.JsObject]])
val status1 = JsonParser.parse(status1)
Run Code Online (Sandbox Code Playgroud)
我不确定,无法弄清楚接下来要做什么,以便迭代数据,提取它并将其呈现到我的网页.
我目前正在将所有Rest测试移动到CI服务器,并注意到所有测试都因SSL握手而失败,现在我已经使用我们的Java测试套件成功禁用了TrustManager,但我不确定如何使用Scala调度库来完成它,并且还没有找到可以在这种情况下应用的许多示例.
val JSONstr = "{samplekey:samplevalue}"
val response:String = Http(url("https://www.host.com/path/to/post")
<< (checkInJSONstr, "application/json") as_str)
Run Code Online (Sandbox Code Playgroud)
正如预期发生以下异常:
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352)
...
Run Code Online (Sandbox Code Playgroud)
有没有办法干净地语法上忽略使用调度库的SSL?
以下是浏览器中的有效查询(例如Firefox):
http://www.freesound.org/api/sounds/search/?q=barking&api_key=074c0b328aea46adb3ee76f6918f8fae
Run Code Online (Sandbox Code Playgroud)
产生一个JSON文档:
{
"num_results": 610,
"sounds": [
{
"analysis_stats": "http://www.freesound.org/api/sounds/115536/analysis/",
"analysis_frames": "http://www.freesound.org/data/analysis/115/115536_1956076_frames.json",
"preview-hq-mp3": "http://www.freesound.org/data/previews/115/115536_1956076-hq.mp3",
"original_filename": "Two Barks.wav",
"tags": [
"animal",
"bark",
"barking",
"dog",
"effects",
...
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用Dispatch 0.9.4执行此查询.这是一个build.sbt:
scalaVersion := "2.10.0"
libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.9.4"
Run Code Online (Sandbox Code Playgroud)
从sbt console,我做以下事情:
import dispatch._
val q = url("http://www.freesound.org/api/sounds/search")
.addQueryParameter("q", "barking")
.addQueryParameter("api_key", "074c0b328aea46adb3ee76f6918f8fae")
val res = Http(q OK as.String)
Run Code Online (Sandbox Code Playgroud)
但承诺总是以下列错误完成:
res0: dispatch.Promise[String] = Promise(!Unexpected response status: 301!)
Run Code Online (Sandbox Code Playgroud)
那么我做错了什么?这是API文档,如果有帮助的话.
有一些关于在发送http://dispatch.databinder.net/Combined+Pages.html中发送帖子请求的文档, 但目前尚不清楚.那里有myRequest和myPost是什么?
我想发送一个https发布请求+通过标题手动添加一些cookie +添加一些海关标题,如表单数据等,然后通过阅读标题和cookie读取响应.
我只知道如何准备发送帖子请求的网址:
val url = host(myUrl + "?check=1&val1=123").secure
Run Code Online (Sandbox Code Playgroud)
接下来我该怎么办?
当我使用Dispatch库和Scala时,出于调试目的,如何在写完这样的语句之后用文本中的标题等打印出整个HTTP请求?
val svc = url("http://api.hostip.info/country.php")
Run Code Online (Sandbox Code Playgroud) 我正在关注调度文档中的第一个示例-
val svc = url("http://api.hostip.info/country.php")
val country = Http(svc OK as.String)
for (c <- country)
println(c)
Run Code Online (Sandbox Code Playgroud)
我没有打印任何输出.当我将其更改为下面以进行阻塞调用时,我得到输出.
val res = country()
println(res)
Run Code Online (Sandbox Code Playgroud)
需要帮助.
完整的程序 -
import dispatch._
object DispatchTest {
def main(args: Array[String]) {
val svc = url("http://api.hostip.info/country.php")
val country = Http(svc OK as.String)
for (c <- country)
println(c)
}
}
Run Code Online (Sandbox Code Playgroud)