This version sends a HEAD request then a GET. It fails with ProtocolException
every time I run it.
@Grab(group='com.squareup.okhttp3', module='okhttp', version='3.11.0')
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
def log = '''
2018-08-24 21:28:26.547 Verbose/KodiLogger: --> [910] User-Agent: Yatse/8.6.0B1 (Linux;Android 4.4.2) ExoPlayerLib/2.8.4
2018-08-24 21:28:26.547 Verbose/KodiLogger: --> [910] Accept-Encoding: identity
'''
def url = 'http://192.168.0.51:8080/vfs/%2Fsrv%2FMusic%2FEverything%20But%20The%20Girl%2FAmplified%20Heart%2F05%20Get%20Me.mp3'
def headers = log
.split('\n')
.collect {
def match = (it =~ /.*\[\d+\] (.*?): (.*)/)
return match? match[0][1,2] : null
}
.grep { it }
println headers.collect { it.join(':') } .join('\n')
OkHttpClient client = new OkHttpClient();
Request.Builder headBuilder = new Request.Builder()
.head().url(url)
Request.Builder getBuilder = new Request.Builder()
.url(url)
headers.each {
headBuilder.header(it[0], it[1])
getBuilder.header(it[0], it[1])
}
Request headRequest = headBuilder.build()
Response headResponse = client.newCall(headRequest).execute();
println "\nHEAD Response:"
println headResponse.headers()
Request getRequest = getBuilder.build()
Response getResponse = client.newCall(getRequest).execute();
println "\nGET Response:"
println getResponse.headers()
Outputs:
User-Agent:Yatse/8.6.0B1 (Linux;Android 4.4.2) ExoPlayerLib/2.8.4
Accept-Encoding:identity
HEAD Response:
Last-Modified: Sat, 13 May 2017 09:57:18 GMT
Expires: Sat, 24 Aug 2019 22:40:09 GMT
Content-Type: audio/mpeg3
Content-Length: 5139987
Cache-Control: public, max-age=31536000
Accept-Ranges: bytes
Date: Fri, 24 Aug 2018 22:40:09 GMT
Caught: java.net.ProtocolException: Unexpected status line:
java.net.ProtocolException: Unexpected status line:
at okhttp3.internal.http.StatusLine.parse(StatusLine.java:69)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at okhttp3.Call$execute.call(Unknown Source)
at yatse-test.run(yatse-test.groovy:45)
Debugging, seems like the offending stream has no header, possibly no content.
It only happens if there is the initial HEAD request, comment that out and the GET works.
Doesn’t seem to depend on the headers.