2013-06-09 5 views
7

Ich versuche, eine einfache HTTPS-Anfrage mit dieser Bibliothek https://github.com/scalaj/scalaj-http zu machen. Die Anfrage enthält einige JSON-Daten. HierSocketTimeoutException, wenn ich Scalaj Anfrage verwenden

ist, was ich tue:

val jsonHeaders = """{"jsonrpc": "2.0", "method": "someMethod", "params": {"dataIds":["12348" , "456"]}, "data2": "777"}""" 

    val result = Http.postData("https://someurl.com/json-rpc", jsonHeaders) 
    .header("content-type", "application/json") 
    .header("X-Application", "myCode") 
    .header("X-Authentication", "myCode2") 
    .option(HttpOptions.readTimeout(10000)) 
    .asString 
    //.responseCode -- the same error 

    println(result) 

Und es gibt immer mir einen Timeout-Fehler:

[error] (run-main) java.net.SocketTimeoutException: connect timed out 
java.net.SocketTimeoutException: connect timed out 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
    at java.net.SocksSocketImpl.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source) 
    at sun.net.NetworkClient.doConnect(Unknown Source) 
    at sun.net.www.http.HttpClient.openServer(Unknown Source) 
    at sun.net.www.http.HttpClient.openServer(Unknown Source) 
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source) 
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source) 
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source) 
    at scalaj.http.Http$$anonfun$3.apply(Http.scala:263) 
    at scalaj.http.Http$$anonfun$3.apply(Http.scala:261) 
    at scalaj.http.Http$Request.process(Http.scala:102) 
    at scalaj.http.Http$Request.apply(Http.scala:90) 
    at scalaj.http.Http$Request.asString(Http.scala:133) 
    at Application$delayedInit$body.apply(Application.scala:27) 
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40) 
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) 
    at scala.App$$anonfun$main$1.apply(App.scala:71) 
    at scala.App$$anonfun$main$1.apply(App.scala:71) 
    at scala.collection.immutable.List.foreach(List.scala:318) 
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32) 
    at scala.App$class.main(App.scala:71) 
    at Application$.main(Application.scala:8) 
    at Application.main(Application.scala) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
[trace] Stack trace suppressed: run last compile:run for the full output. 
java.lang.RuntimeException: Nonzero exit code: 1 
    at scala.sys.package$.error(package.scala:27) 

Wenn ich nur

val jsonHeaders = """{"version123": "12.0", "method": "someMethod", "params": {"dataIds": ["12348" , "456"]}, "data2": "777"}""" 
val result = Http.postData("https://someurl.com/some-url2", jsonHeaders) 
     .header("content-type", "application/json") 
     .header("X-Application", "myCode") 
     .header("X-Application1234", "myCode2") 
     .option(HttpOptions.readTimeout(10000)) 
println(result) 

kehrt

Request(POST,<function2>,<function1>,List(),List((X-Authentication,myCode2), (X-Application1234,myCode), (content-type,application/json)),List(<function1>, <function1>, <function1>),DIRECT) 

Was mache ich falsch und gibt es eine weitere einfache Möglichkeit, https-Anfrage zu senden? Auch wenn beteiligt spray Rahmen ist, wäre es in Ordnung (Ich finde kein Beispiel dafür, wie man das in spray, obwohl).

UPDATE:

Ein Beispiel wurde von hier genommen Doing HTTP request in Scala

+0

wie Ihre zweite Anfrage Sieht keine tatsächliche Anforderung tun - es baut nur Request-Objekt, das dann durch 'asString' ausgelöst werden. Sind Sie sicher, dass die Anforderung innerhalb des angegebenen Zeitlimits erfüllt werden kann (z. B. wenn Sie versucht haben, dasselbe mit curl zu tun). –

+0

@ om-nom-nom Ich kann das Timeout auskommentieren und nichts wird sich ändern. –

+0

@ om-nom-nom schau auf mein update. Ich kann diese Anfrage auch in Python machen und es wird gut funktionieren. –

Antwort

11

Sie müssen sich mit einem Verbindungs-Timeout neben Ihrem aktuellen Lese Timeout angeben: .option(HttpOptions.connTimeout(10000)).option(HttpOptions.readTimeout(50000)). Ändern Sie die 10000 auf einen Wert, der für Sie arbeitet. Das Standard-Verbindungstimeout ist ein ziemlich aggressives 100.

Sie haben bereits ein Lese-Timeout angegeben, aber die Ausnahme besagt, dass beim Herstellen der Verbindung eine Zeitüberschreitung auftritt, nicht beim Lesen der Antwort.

docs Siehe: https://github.com/scalaj/scalaj-http#custom-connect-and-read-timeouts

+0

hat es nicht geholfen. Selbst wenn ich 60000 10-mal vergrößere, scheint es, als würde es ein Auge zudrücken - es gibt überhaupt keinen Unterschied, was bedeutet, dass es mit dem Fehler so schnell wie vorher existiert. –

+0

vielleicht habe ich die Reihenfolge der Argumente verwechselt? –

+0

Seltsam. Nur um zu überprüfen: Sie geben sowohl ein 'HttpOptions.readTimeout' als auch ein' HttpOptions.connTimeout' an? Ich glaube nicht, dass die Reihenfolge einen Unterschied machen sollte. Hier ist ein Beispiel, wie ich beide verwendet habe: https://github.com/theon/xsbt-coveralls-plugin/blob/master/src/main/scala/com/github/theon/coveralls/CoverallsClient.scala# L48. – theon

Verwandte Themen