java.net.URLConnection在这里经常询问使用情况,Oracle教程对此非常简洁.
该教程基本上只显示了如何触发GET请求并读取响应.它没有解释如何使用它来执行POST请求,设置请求标头,读取响应标头,处理cookie,提交HTML表单,上传文件等.
那么,我如何使用java.net.URLConnection触发和处理"高级"HTTP请求?
在过去的几周里,我们的客户开始看到100个这样的"SSLException错误 - 连接重置连接",我无法弄清楚为什么
我们在okhttp上使用Retrofit,没有特殊配置
public class OkHttpClientProvider implements IOkHttpClientProvider {
OkHttpClient okHttpClient;
public OkHttpClientProvider() {
this.okHttpClient = createClient();
}
public OkHttpClient getOkHttpClient() {
return this.okHttpClient;
}
private OkHttpClient createClient() {
return new OkHttpClient();
}
}
Run Code Online (Sandbox Code Playgroud)上述客户提供商是单身人士.RestAdapter是使用这个注入的客户端构建的(我们使用匕首) -
RestAdapter.Builder restAdapterBuilder = new RestAdapter.Builder()
.setConverter(converter)
.setEndpoint(networkRequestDetails.getServerUrl())
.setClient(new OkClient(okHttpClientProvider.getOkHttpClient()))
.setErrorHandler(new NetworkSynchronousErrorHandler(eventBus))
);
Run Code Online (Sandbox Code Playgroud)
基于堆栈溢出解决方案,我发现了 -
服务器上的保持活动持续时间为180秒,OkHttp的默认值为300秒
服务器在其标头中返回"Connection:close",但客户端请求发送"Connection:keepAlive"
服务器支持TLS 1.0/1.1/1.2并使用Open SSL
我们的服务器最近已转移到另一个托管服务提供商的另一个地理位置,因此我不知道这些是否是DNS故障
我们尝试调整keepAlive之类的东西,在服务器上重新配置OpenSSL,但由于某种原因,Android客户端不断收到此错误
当您尝试使用应用程序发布内容或拉动刷新时,它会立即发生,并且不会出现任何延迟(在发生此异常之前,它甚至不会进入网络或延迟,这意味着连接已经断开).但尝试多次以某种方式"修复它",我们取得了成功.它会在以后再次发生
我们已经在服务器上使我们的DNS条目无效,看看这是否是由它引起的,但是没有帮助
它主要发生在LTE上,但我也在Wifi上看过它
我不想禁用keep alive,因为大多数现代客户都不这样做.此外,我们正在使用OkHttp 2.4,这是后冰淇淋三明治设备的一个问题,所以我希望它应该照顾这些潜在的网络问题.iOS客户端也获得了这些例外但接近100倍(iOS客户端使用AFNetworking 2.0).我正在努力寻找新的东西,在这一点上尝试,任何帮助/想法?
更新 - 通过okhttp添加完整堆栈跟踪
retrofit.RetrofitError: Read error: ssl=0x9dd07200: I/O error during system call, Connection reset by …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的Android应用程序连接到本地主机URL,这要归功于wamp服务器,但它不起作用.我的目标是获取json数据并解析这些数据.对于我的测试,我使用的设备不是模拟器,我在AndroidManifest.xml中使用权限:
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)
我的网址看起来像这样:
String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
Run Code Online (Sandbox Code Playgroud)
我试过了 :
http://localhost/
http://10.0.2.2:8080/
http://10.0.2.2/
Run Code Online (Sandbox Code Playgroud)
但它到目前为止从未奏效:
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)
Run Code Online (Sandbox Code Playgroud)
然后我尝试在互联网上找到一个json url测试:http://headers.jsontest.com/
它工作得很好,我在这个地址得到了json数据.所以我想我的代码很好,这里的问题是我的localhost网址,我不知道应该是什么样的确切形式..我读了很多关于它的线程,但我没有找到解决方案.
这是我的代码:
主要活动 :
public class MainActivity extends Activity {
private String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
private ListView lv = null; …Run Code Online (Sandbox Code Playgroud) 我已经搜索了很多这个例外,但是我没有找到任何可以解决我的问题的相关答案,我正在使用HttpURLConnection从网址获取xml的响应,它工作正常但有时我得到这个例外:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer),我使用了以下代码,url1是我的url,它给出了一个xml.
url=new URL(url1);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.connect();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String result, line = reader.readLine();
result = line;
while((line=reader.readLine())!=null)
{
result+=line;
}
System.out.println("Result: "+result);
Run Code Online (Sandbox Code Playgroud) 我在 Android 中有一个 HTTP 服务器。我为每个 HTTP 请求创建一个新线程,如提到的链接中所示:
当我发出多个 GET 请求时,有时会收到如下异常:
01-22 10:28:22.779: W/System.err(2019): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
01-22 10:28:22.779: W/System.err(2019): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
01-22 10:28:22.779: W/System.err(2019): at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
01-22 10:28:22.779: W/System.err(2019): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
01-22 10:28:22.779: W/System.err(2019): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
01-22 10:28:22.784: W/System.err(2019): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.HttpRequestParser.parseHead(HttpRequestParser.java:71)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.AbstractHttpServerConnection.receiveRequestHeader(AbstractHttpServerConnection.java:141)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:135)
01-22 10:28:22.784: …Run Code Online (Sandbox Code Playgroud) 我想使用 androidVpnService来捕获数据包并根据 IP 地址过滤它们。我可以很好地从“tun”接口获取数据包,但之后我不确定如何将它们转发到原始目的地。根据这个答案的评论,我似乎只需要:
我尝试像这样发送数据:
Socket socket = new Socket();
socket.bind(new InetSocketAddress(0));
if (protect(socket)){
Log.e(TAG, "Socket protected");
}else{
Log.e(TAG, "Socket NOT protected");
}
socket.connect(new InetSocketAddress(ipPacket.getDestinationIp(), ipPacket.getDstPort()));
Log.e(TAG, "Socket connected: " + socket.isConnected());
socket.getOutputStream().write(getTCPHeader(getIpHeader(packet)[1])[1].array());
Run Code Online (Sandbox Code Playgroud)
getTCPHeader(ByteArray packet)这些方法getIpHeader(ByteArray packet)简单地将数据包分成两个,ByteArray如下所示:
private ByteBuffer[] getIpHeader(ByteBuffer packet){
packet.position(0);
ByteBuffer ipHeader = ByteBuffer.allocate(20);
ByteBuffer data = ByteBuffer.allocate(packet.limit() - 20);
packet.get(ipHeader.array(), 0, 20);
packet.get(data.array(), 0, packet.limit() - 20); …Run Code Online (Sandbox Code Playgroud) 在使用防护 改造时面临问题
HTTP FAILED: java.net.SocketException: recvfrom failed: ECONNRESET
(Connection reset by peer)
Run Code Online (Sandbox Code Playgroud)
我知道这个问题已被提出,但我面临的这个错误是来自其他场景...找到与此错误相关的链接,但我得到的没有任何正确的解决方案.
在Android中获取"SocketException:Connection by peer reset"
https://github.com/square/retrofit/issues/1027
我Retrofit HTTP client通过使用body请求用于api调用.
@POST("users/new")
Call<User> createUser(@Body User user);
Run Code Online (Sandbox Code Playgroud)
我的客户
public static OkHttpClient getClient() {
if (client == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
RequestInterceptor m1RequestInterceptor = new RequestInterceptor();
client = new OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.addInterceptor(interceptor)
.addInterceptor(m1RequestInterceptor)
.build();
}
return client;
}
Run Code Online (Sandbox Code Playgroud)
保护规则
# Retain generic type information for use by reflection by converters and adapters. …Run Code Online (Sandbox Code Playgroud) 我是android的新手.通过HttpClient更新时遇到错误,如java.net.SocketException:recvfrom失败:ECONNRESET(连接由对等方重置)
public static String newupdateProf(String memid, String gender,
String pdata, String dob, String pimg) throws IOException,
JSONException {
// System.setProperty("http.keepAlive", "false");
final HttpClient httpClient = new DefaultHttpClient();
final HttpPost httpost = new HttpPost(websrv.trim() + "/newUpdtProf");
JSONStringer img = new JSONStringer().object().key("prof").object()
.key("memid").value(memid.trim()).key("gender")
.value(gender.trim()).key("pdata").value(pdata.trim())
.key("dob").value(dob.trim()).key("pimg").value(pimg.trim())
.endObject().endObject();
StringEntity se = new StringEntity(img.toString());
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpClient.execute(httpost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream stream = httpEntity.getContent();
String result = convertStreamToString(stream);
return result.trim();
}
Run Code Online (Sandbox Code Playgroud)
请帮我.我也试过了
System.setProperty("http.keepAlive", "false");
Run Code Online (Sandbox Code Playgroud)
但它不起作用.