当前位置: 首页 > 知识库问答 >
问题:

HttpUrlConnection ger响应代码给出连接重置异常

单于耘豪
2023-03-14

当我试图打骗局的时候。getResponseCode()我收到连接重置异常,但在web浏览器中点击相同的url时,它正确返回了响应,但从我的代码中没有返回响应。请帮我检查一下。

This is the code:-

private HttpURLConnection con;
private String directory;

public MIMESender(String url_path,String provider,String user,String password) throws Exception {
    super();
    // TODO Auto-generated constructor stub
    URL url = new URL(url_path);
    System.out.println("Url Path=====" + url_path);
    con = (HttpURLConnection) url.openConnection();

    con.setRequestMethod("POST");

    con.setDoOutput(true);

    con.setRequestProperty("X-XIAM-ContentProvider",provider);
    con.setRequestProperty("X-XIAM-ContentUsername",user);
    con.setRequestProperty("X-XIAM-ContentPassword",password);
    //System.setProperty("http.keepAlive", "false");
    //System.out.println("Keep Alive=========" + System.getProperty("http.keepAlive"));
}

public void setContentType(String type) throws IOException
{
    con.setRequestProperty("Content-Type",type);
}

public static MimeBodyPart createXMLPart(String xml) throws MessagingException
{
    ByteArrayDataSource fileData = new ByteArrayDataSource(xml.getBytes(),"text/xml");
    DataHandler fileDataHandler = new DataHandler(fileData);
    MimeBodyPart body = new MimeBodyPart();
    body.setDataHandler(fileDataHandler);
    body.setHeader("Content-Type",fileData.getContentType());
    return body;
}

public static MimeBodyPart createBodyPart(String filename) throws MessagingException
{
    File file = new File(filename);
    return createBodyPart(file);
}

public static MimeBodyPart createBodyPart(File file) throws MessagingException
{
    FileDataSource fileData = new FileDataSource(file);
    DataHandler fileDataHandler = new DataHandler(fileData);
    MimeBodyPart body = new MimeBodyPart();
    body.setDataHandler(fileDataHandler);
    body.setHeader("Content-Type",fileData.getContentType());
    if(!fileData.getContentType().startsWith("text"))
        body.setHeader("Content-Transfer-Encoding","base64");
    else body.setHeader("Content-Transfer-Encoding","7bit");
    body.setContentID(file.getName());
    body.setHeader("Content-Location",file.getName());
    body.setHeader("Content-Disposition","attachment; filename="+file.getName());      
    return body;
}

public static MimeBodyPart createBodyPart(String location,File file) throws MessagingException
{
    FileDataSource fileData = new FileDataSource(file);
    DataHandler fileDataHandler = new DataHandler(fileData);
    MimeBodyPart body = new MimeBodyPart();
    body.setDataHandler(fileDataHandler);
    body.setHeader("Content-Type",fileData.getContentType());
    if(!fileData.getContentType().startsWith("text"))
        body.setHeader("Content-Transfer-Encoding","base64");
    body.setContentID(file.getName());
    body.setHeader("Content-Location",location);
    body.setHeader("Content-Disposition","attachment; filename="+file.getName());   
    return body;
}

public String sendItem(String xml,String location,String filename) throws MessagingException,IOException
{

    String response = null;
    MimeMultipart mime = new MimeMultipart();
    mime.setSubType("related");                     
    String type = mime.getContentType();
    setContentType(type);

    mime.addBodyPart(MIMESender.createXMLPart(xml));

    if(!filename.equals(""))
    {   
        File file = new File(filename);
        mime.addBodyPart(MIMESender.createBodyPart(location,file));
    }
    response = submit(mime);
    return response;
}

public String send(String xml,String location,String filename) throws MessagingException,IOException
{
    String response = null;
    response = send(xml,location,new File(filename));
    return response;
}

public String send(String xml,String location,File file) throws MessagingException,IOException
{
    String response = null;
    File[] files = new File[1];
    files[0] = file;
    response = send(xml,location,files);
    return response; 
}

public String send(String xml,String location,File[] files) throws MessagingException,IOException
{
    String response = null;

    try
    {

    MimeMultipart mime = new MimeMultipart();
    mime.setSubType("related");         
    String type = mime.getContentType();
    setContentType(type);

    mime.addBodyPart(MIMESender.createXMLPart(xml));

    MimeMultipart mime_mix = new MimeMultipart();
    mime_mix.setSubType("mixed");
    for(int i=0;i<files.length;i++)
    {
        System.out.println("files : " + files[i]);
        mime_mix.addBodyPart(MIMESender.createBodyPart(files[i]));
    }

    MimeBodyPart body_mix = new MimeBodyPart();
    body_mix.setContent(mime_mix);
    body_mix.setHeader("Content-Type",mime_mix.getContentType());
    body_mix.setHeader("Content-Location",location);
    mime.addBodyPart(body_mix);

    response = submit(mime);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return response;
}

public String send(String xml,String location,File[] files,String preloc,String preview) throws MessagingException,IOException
{
    String response = null;
    MimeMultipart mime = new MimeMultipart();
    mime.setSubType("related");         
    String type = mime.getContentType();
    setContentType(type);

    mime.addBodyPart(MIMESender.createXMLPart(xml));

    if(location!=null)
    {
        System.out.println("Inside Location not equals to null");
        MimeMultipart mime_mix = new MimeMultipart();
        mime_mix.setSubType("mixed");
        for(int i=0;i<files.length;i++)
        {
            System.out.println(files[i].getAbsolutePath() + " - " + files[i].exists());
            mime_mix.addBodyPart(MIMESender.createBodyPart(files[i]));
        }

        MimeBodyPart body_mix = new MimeBodyPart();
        body_mix.setContent(mime_mix);
        body_mix.setHeader("Content-Type",mime_mix.getContentType());
        body_mix.setHeader("Content-Location",location);
        mime.addBodyPart(body_mix);
    }

    if(preloc!=null){
    File file = new File(preview);
    mime.addBodyPart(MIMESender.createBodyPart(preloc,file));
    }
    response = submit(mime);
    return response;
}

public String submit(MimeMultipart mime) throws IOException, MessagingException
{
    String response=null;
    DataOutputStream dos = new DataOutputStream(con.getOutputStream());
    dos.writeBytes("\r\n\r\n");
    mime.writeTo(dos);
    dos.flush();
    dos.close();        
    System.out.println("Connection : "+con);
    String httpCode = String.valueOf(con.getResponseCode()); // I am getting exception here..
    String respMsg = con.getResponseMessage();
    response = "HTTP/1.1 "+httpCode+" "+respMsg+"\n";

    int length = con.getContentLength();
    byte[] cnt = new byte[length];
    DataInputStream in = new DataInputStream(con.getInputStream());
    in.readFully(cnt);
    response = response + new String(cnt).replaceAll("&lt;","<").replace("&gt;",">") + "\n";

    con.disconnect();
    return response;
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
        String url = args[0];
        MIMESender sender = new MIMESender(url,"CP001","CP001","CP001");
        File file = new File(args[1]);
        FileInputStream in = new FileInputStream(file);
        int len = (int)file.length();
        byte[] data = new byte[len];
        in.read(data);

        int n = args.length - 3;
        File[] files = new File[n];
        for(int i=0;i<n;i++)
            files[i] = new File(args[3+i]);
        String resp = sender.sendItem(new String(data),args[2],args[2]);
        System.out.println(resp);

    } catch(IOException io){io.printStackTrace();
    } catch(MessagingException msg){msg.printStackTrace();      
    } catch(Exception e){e.printStackTrace();}
}

public String getDirectory() {
    return directory;
}

public void setDirectory(String directory) {
    this.directory = directory;
}

我的完整堆栈跟踪是:-

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at com.nokia.content.misc.MIMESender.submit(MIMESender.java:218)
at com.nokia.content.misc.MIMESender.send(MIMESender.java:201)
at com.nokia.content.CTUploader.sendSubItem(CTUploader.java:1202)
at com.nokia.content.CTUploader.send(CTUploader.java:800)
at com.nokia.content.CTUploader.send(CTUploader.java:502)
at com.nokia.content.ui.CPA.doSubmission(CPA.java:727)
at com.nokia.content.ui.CPA.submitMenuItemActionPerformed(CPA.java:571)
at com.nokia.content.ui.CPA.access$16(CPA.java:563)
at com.nokia.content.ui.CPA$9.actionPerformed(CPA.java:195)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

请帮我解决这个问题,伙计们...谢谢...

共有1个答案

郗阳德
2023-03-14

由于任何人都不太可能仅基于代码来确定您的问题,因此我将给出如何继续的建议。

考虑到它可以从浏览器中工作,但不能从您的代码中工作,问题很可能是您的代码发送的事务与浏览器发送的事务不完全相同。最可能的差异是缺少或额外的HTTP标头,或者用户代理标头中的差异导致服务器关闭连接。

调试的最简单方法是使用Wireshark来捕获流量并确定浏览器和代码之间的差异。如果您发现存在重大差异并且仍然需要帮助,请发布另一个显示相关差异的问题。

 类似资料:
  • 我是JMeter的新手,正在尝试使用JMeter和SMTP发送HTTP请求和电子邮件。 我将JMeter错误作为非HTTP响应代码: < code > Java . net . socket exception/Non HTTP响应消息:连接重置 SMTP错误为500/ 我已完成以下设置: user.properties: hc。参数: 这些更改仍然没有解决我的问题。你能帮助我在哪里更改设置来解决

  • 我正在尝试用JMeter学习负载测试(非常新)。使用该教程在JMeter网站和另一个youtube教程上构建一个Web测试计划,我创建了一个HTTP GET请求来访问www.google.com/,但是我得到了以下结果: 这是我的要求:链接到图像,因为我还没有10个声誉 环顾四周,其他人正在遇到此问题,但他们的问题是在 250 个用户之后: 响应代码: 非 HTTP 响应代码: java.net.

  • 我正在使用 Jmeter 执行一个脚本进行负载测试。我在两者之间遇到错误,例如。如果我应用了500users的负载,直到250个用户线程成功运行,那么错误来自连接超时错误。然后,它再次运行成功为某些线程,然后出错。 代码如下:- 响应标头: 我需要破坏服务器。 有人能帮我吗?

  • 我正在尝试使用JMeter向在本地主机上运行的应用程序发送请求,但由于java.net.Socket异常,许多请求都失败了。不过,我在控制台中没有看到任何异常。 我读了这些问题1和问题2,但没有多大帮助。 我的代码如下: 存储库 我的 JMeter 配置的屏幕截图

  • 问题内容: 我想从这里使用以下代码: 如何在当前python会话中保存所有变量? 但是它给出了以下错误: 你能帮我吗? 谢谢! 问题答案: 从您的追溯来看,您似乎正在尝试从函数内部运行该代码。 但是在 当前本地范围内 查找名称。因此,如果在函数内部定义,它将在中而不是中。 您可能想要这样的东西: 请注意,当从函数 内部 调用时,它将从 定义 函数的模块返回变量,而不是从其 调用的位置返回 。 因此

  • 当使用JMeter/Blazemeter运行测试时,我在测试开始后10-20分钟的某个时间段内随机收到以下错误。 < li >非HTTP响应代码:org . Apache . HTTP . conn . httphostconnectexception < li>500内部服务器错误 < li >非HTTP响应代码:org . Apache . HTTP . truncatedchunkexcep