Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpURLConnection引发连接重置异常_Java_Httpurlconnection_Connection Reset - Fatal编程技术网

Java HttpURLConnection引发连接重置异常

Java HttpURLConnection引发连接重置异常,java,httpurlconnection,connection-reset,Java,Httpurlconnection,Connection Reset,我正在开始java网络编程,但以下代码总是出现连接重置异常: import java.io.*; import java.net.*; import java.util.*; public class Net { public static void main() { try { // this not working ... URL url = new URL("http://localhost/test.php

我正在开始java网络编程,但以下代码总是出现连接重置异常:

import java.io.*;
import java.net.*;
import java.util.*;

public class Net {

    public static void main() 
    {

        try
        {
            // this not working ...  URL url = new URL("http://localhost/test.php");

            // not even the following trial
            URL url = new URL("http://www.google.com.gh/");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();


            // Exception is thrown here
            InputStream in = conn.getInputStream();
        }

        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}

我在学习过程中尝试过的所有例子都失败了。我不知道怎么了。请帮助。

您是否尝试设置其他信息

  conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded");

  conn.setRequestProperty("Content-Length", "" + 
           Integer.toString(urlParameters.getBytes().length));
  conn.setRequestProperty("Content-Language", "en-US");  

  conn.setUseCaches (false);
  conn.setDoInput(true);
  conn.setDoOutput(true);


 DataOutputStream wr = new DataOutputStream (
              connection.getOutputStream ());
  wr.writeBytes (urlParameters);
  wr.flush ();
  wr.close ();


 The urlParameters is a URL encoded string.

 String urlParameters =
    "fName=" + URLEncoder.encode("???", "UTF-8") +
    "&lName=" + URLEncoder.encode("???", "UTF-8")

您是否尝试设置一些其他信息

  conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded");

  conn.setRequestProperty("Content-Length", "" + 
           Integer.toString(urlParameters.getBytes().length));
  conn.setRequestProperty("Content-Language", "en-US");  

  conn.setUseCaches (false);
  conn.setDoInput(true);
  conn.setDoOutput(true);


 DataOutputStream wr = new DataOutputStream (
              connection.getOutputStream ());
  wr.writeBytes (urlParameters);
  wr.flush ();
  wr.close ();


 The urlParameters is a URL encoded string.

 String urlParameters =
    "fName=" + URLEncoder.encode("???", "UTF-8") +
    "&lName=" + URLEncoder.encode("???", "UTF-8")

当您在这台电脑上浏览时,是否定义了代理?如果是这样,您当然也需要在代码中定义代理。看不到代理NickDK。这是一个直接的联系。对我来说很好(稍加修改)。可能是权限问题?可能是,但我猜不出可能是什么。你的代码对我来说很好。看起来确实存在一些本地连接问题。您是否使用任何可公开访问的url获得相同的结果?当您在这台电脑上浏览时,是否定义了代理?如果是这样,您当然也需要在代码中定义代理。看不到代理NickDK。这是一个直接的联系。对我来说很好(稍加修改)。可能是权限问题?可能是,但我猜不出可能是什么。你的代码对我来说很好。看起来确实存在一些本地连接问题。使用任何可公开访问的url都会得到相同的结果吗?我在其他示例中使用了setDoInput、setDoOutput和setUseCaches,但不起作用。我包括了其他参数,但即使设置了请求参数,仍然无法工作。不知道为什么。你需要先发送一个请求,我编辑了我的答案,这行吗?现在行了。问题出在我的防病毒软件上。我想是它堵塞了连接。我停止了杀毒软件,现在它可以工作了。在其他示例中,我有setDoInput、setDoOutput和setUseCaches,但没有工作。我包括了其他参数,但即使设置了请求参数,仍然无法工作。不知道为什么。你需要先发送一个请求,我编辑了我的答案,这行吗?现在行了。问题出在我的防病毒软件上。我想是它堵塞了连接。我停止了杀毒软件,它现在可以工作了。