程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了HttpURLConnection POST,conn.getOutputStream()引发异常大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决httpURLConnection POST,conn.getOutputStream()引发异常?

开发过程中遇到httpURLConnection POST,conn.getOutputStream()引发异常的问题如何解决?下面主要结合日常开发的经验,给出你关于httpURLConnection POST,conn.getOutputStream()引发异常的解决方法建议,希望对你解决httpURLConnection POST,conn.getOutputStream()引发异常有所启发或帮助;

根本无法访问该URL。URL错误或DNS服务器无法解析主机名。尝试使用众所周知的URL进行简单连接以排除一个和另一个,例如

inputStream response = new URL("http://stackoverflow.com").openStream();
// Consume response.

,您需要使用代理服务器进行http连接。您还需要在Java端进行配置 尝试连接到URL 之前, 添加以下行。

System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");

在运行时仅执行一次即可。

也可以看看:

  • Java指南-网络和代理

解决方法

我想使用httpURLConnection进行POST。我以两种方式尝试这种方法,但是这样做总是让我兴奋不已:conn.getOutputStream();

我在这两种情况下得到的异常是:

功能1:

public void makePost(String title,String comment,File filE) {
    try {
        URL servlet = new URL("http://" + "www.server.com/daten/web/test/testupload.nsf/upload?CreateDocument");            
        httpURLConnection conn=(httpURLConnection)servlet.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        String boundary = "---------------------------7d226f700d0";
        conn.setrequestProperty("Content-type","multipart/form-data; boundary=" + boundary);
        //conn.setrequestProperty("Referer","http://127.0.0.1/index.jsp");
        conn.setrequestProperty("Cache-Control","no-cache");

        OutputStream os = conn.getOutputStream(); //exception throws here!
        DataOutputStream out = new DataOutputStream(os);
        out.writeBytes("--" + boundary + "\r\n");
        writeParam(INPUT_titlE,title,out,boundary);
        writeParam(INPUT_COMMENT,comment,boundary);
        writeFile(INPUT_FILE,file.getName(),boundary);
        out.flush();
        out.close();

        InputStream stream = conn.geTinputStream();
        BufferedInputStream in = new BufferedInputStream(stream);
        int i = 0;            
        while ((i = in.read()) != -1) {
            System.out.write(i);            
        }            
        in.close();
    } catch (Exception E) {  
        e.printStackTrace();
    }
}

或功能2:

public void makePost2(String title,File filE) {

    File binaryFile = file;
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

    URLConnection connection = null;
    try {
        connection = new URL("http://" + "www.server.com/daten/web/test/testupload.nsf/upload?CreateDocument").openConnection();
    } catch (MalformedURLException E) {
        e.printStackTrace();
    } catch (IOException E) {
        e.printStackTrace();
    }
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setrequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
    PrintWriter writer = null;
    try {
        OutputStream output = connection.getOutputStream(); //exception throws here
        writer = new PrintWriter(new OutputStreamWriter(output,CHARSET),truE); // true = autoFlush,important!

        // Send normal param.
        writer.println("--" + boundary);
        writer.println("Content-Disposition: form-data; name=\""+ INPUT_titlE +"\"");
        writer.println("Content-Type: text/plain; charset=" + CHARSET);
        writer.println();
        writer.println(titlE);

//        Send binary file.
        writer.println("--" + boundary);
        writer.println("Content-Disposition: form-data; name=\""+ INPUT_FILE +"\"; filename=\"" + binaryFile.getName() + "\"");
        writer.println("Content-Type: " + URLConnection.guessContentTypeFromname(binaryFile.getName()));
        writer.println("Content-Transfer-Encoding: binary");
        writer.println();
        InputStream input = null;
        try {
            input = new FileInputStream(binaryFilE);
            byte[] buffer = new byte[1024];
            for (int length = 0; (length = input.read(buffer)) > 0;) {
                output.write(buffer,length);
            }
            output.flush(); // Important! Output cAnnot be closed. Close of writer will close output as well.
        } catch (IOException E) {
            e.printStackTrace();
        } finally {
            if (input != null) try { input.close(); } catch (IOException logOrIgnorE) {}
        }
        writer.println(); // Important! InDicates end of binary boundary.

        // End of multipart/form-data.
        writer.println("--" + boundary + "--");
    } catch (IOException E) {
        e.printStackTrace();
    } finally {
        if (writer != null) writer.close();
    }


}

大佬总结

以上是大佬教程为你收集整理的HttpURLConnection POST,conn.getOutputStream()引发异常全部内容,希望文章能够帮你解决HttpURLConnection POST,conn.getOutputStream()引发异常所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。