下面这个方法,之前在eclipse上用一直没问题,后面不知道做了什么,这个方法就一直报错
          
但是我把项目放到服务器上去,就没问题,放到我同事的电脑上也没问题,所以我觉得应该是我的电脑缺少了什么东西。
有没有谁遇到过这样的情况,我开发的时候很苦恼,我现在得到的有可能解决的方法是重装系统,但是我并不想重装啊!!!
谁能帮忙解决一下,万分感激啊,这是第一次发帖子!   /**
  * 用form表单形式post访问接口,返回json
  * @param url
  * @param params
  * @return response.toString()
  */
 public static String formPost(String url,String params){
        String responseMessage = ""; 
        StringBuffer response = new StringBuffer(); 
        HttpURLConnection httpConnection = null; 
        OutputStreamWriter out = null; 
        BufferedReader reader = null; 
        try { 
          URL urlPost = new URL(url); 
          httpConnection = (HttpURLConnection) urlPost.openConnection(); 
          httpConnection.setDoOutput(true); 
          httpConnection.setDoInput(true); 
          // 参数长度太大,不能用get方式 
          httpConnection.setRequestMethod("POST"); 
          // 不使用缓存 
          httpConnection.setUseCaches(false); 
          // URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数 
          httpConnection.setInstanceFollowRedirects(true); 
          // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的 
          // 意思是正文是urlencoded编码过的form参数 
          httpConnection.setRequestProperty("Connection", "Keep-Alive");
          // 设置请求头信息
          httpConnection.setRequestProperty("Charset", "UTF-8");
          // 设置边界
          //httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");            // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成, 
          // 要注意的是connection.getOutputStream会隐含的进行connect。 
          // 实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。            httpConnection.connect(); 
          out = new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8"); 
          // 构建请求参数  
            StringBuffer sb = new StringBuffer(); 
            sb.append(params);
          out.write(sb.toString()); 
          System.out.println("send_url:" + url);  
          System.out.println("send_data:" + sb.toString());  
          // flush and close 
          out.flush();          } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
             try { 
                if (null != out) { 
                  out.close(); 
                } 
                if (null != reader) { 
                  reader.close(); 
                } 
                if (null != httpConnection) { 
                  httpConnection.disconnect(); 
                } 
              } catch (Exception e2) { 
                  e2.printStackTrace();
              }
        }           try {
          reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"UTF-8")); 
          while ((responseMessage = reader.readLine()) != null) { 
              response.append(responseMessage); 
              response.append("\n"); 
          }            if (!"failure".equals(response.toString())) { 
              System.out.println("post链接success");
          } else { 
              System.out.println("post链接failue");
          } 
          // 将该url的配置信息缓存起来 
          return response.toString(); 
        } catch (IOException e) {
            e.printStackTrace();
      return response.toString(); 
        }
 }

解决方案 »

  1.   

      你把jdk重新装一遍不就行了?干嘛要重装系统
      

  2.   

    加密算法协商中出的问题 貌似在jdk6中才有,升级jdk试试
      

  3.   

    访问的是https请求吗?
      

  4.   


    这个问题重装jdk就能解决了吗?那我晚上回去试试
      

  5.   


     httpConnection.connect();  这一句,贴出来的代码里面 的36行
      

  6.   


    我已经是jdk1.8 了,我晚上重装一下试试
      

  7.   

    回复不了私信,网上有示例HttpClient如何绕过证书验证,你查一下相关资料
      

  8.   


    private static final SSLHandler simpleVerifier = new SSLHandler();
    private static SSLSocketFactory sslFactory;public static void formPost(String url,String params){
        ......
        httpConnection.setSSLSocketFactory(getSSLSF());
        httpConnection.setHostnameVerifier(simpleVerifier);
        ......
    }
    public static synchronized SSLSocketFactory getSSLSF() throws Exception
    {
    if(sslFactory!=null) return sslFactory; 
    SSLContext sc = SSLContext.getInstance("SSLv3");
    sc.init(null, new TrustManager[]{simpleVerifier}, null);
    sslFactory = sc.getSocketFactory();
    return sslFactory;
    }
        
        private static class SSLHandler implements X509TrustManager,HostnameVerifier
    {
    private SSLHandler()
    {
    }

    public void checkClientTrusted(X509Certificate[] arg0, String arg1)
    throws CertificateException {
    } public void checkServerTrusted(X509Certificate[] arg0, String arg1)
    throws CertificateException {
    } public X509Certificate[] getAcceptedIssuers() {
    return null;
    } public boolean verify(String arg0, SSLSession arg1)
    {
    return true;
    }
    }
      

  9.   


    是的  https会出这样的问题,http不会谢谢你的回复,我这里之前有写过这种方法,现在也是不能用。public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {

    JSONObject jsonObject = null;

    try {
    // 创建SSLContext对象,并使用我们指定的信任管理器初始化
    TrustManager[] tm = { new HttpsX509TrustManager() };
    SSLContext sslContext = SSLContext.getInstance("SSLv3", "SunJSSE");
    sslContext.init(null, tm, new java.security.SecureRandom());
    // 从上述SSLContext对象中得到SSLSocketFactory对象
    SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setSSLSocketFactory(ssf);

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    // 设置请求方式(GET/POST)
    conn.setRequestMethod(requestMethod); // 当outputStr不为null时向输出流写数据
    if (null != outputStr) {
    OutputStream outputStream = conn.getOutputStream();
    // 注意编码格式
    outputStream.write(outputStr.getBytes("UTF-8"));
    outputStream.close();
    } // 从输入流读取返回内容
    InputStream inputStream = conn.getInputStream();
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String str = null;
    StringBuffer buffer = new StringBuffer();
    while ((str = bufferedReader.readLine()) != null) {
    buffer.append(str);
    } // 释放资源
    bufferedReader.close();
    inputStreamReader.close();
    inputStream.close();
    inputStream = null;
    conn.disconnect();
    jsonObject = JSONObject.fromObject(buffer.toString());
    } catch (ConnectException ce) {
    ce.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return jsonObject;
    }