月半花开 2017-12-11 04:27 采纳率: 0%
浏览 1320

java http地址,发送post请求,eclipse运行没问题,打包成jar运行报错

java.io.IOException: Server returned HTTP response code: 400 for URL: http://113.204.53.18:8091/innosill/esb/schedule/receivingcarrierplan
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.hezhi.kylwsp.ui.stationCoordination.LinePlanManageUi.doPost(LinePlanManageUi.java:615)
at com.hezhi.kylwsp.ui.stationCoordination.LinePlanManageUi.commitBtnMedth(LinePlanManageUi.java:566)
at com.hezhi.kylwsp.ui.stationCoordination.LinePlanManageUi.BtnComMit(LinePlanManageUi.java:475)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.hezhi.ui.composite.button.HZbutton$1.widgetSelected(HZbutton.java:68)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
at com.hezhi.kylwsp.ui.main.Main.open(Main.java:136)
at com.hezhi.kylwsp.ui.main.LoginDialog$4.keyPressed(LoginDialog.java:326)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:161)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
at org.eclipse.swt.custom.CCombo.textEvent(CCombo.java:1693)
at org.eclipse.swt.custom.CCombo$1.handleEvent(CCombo.java:111)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1103)
at org.eclipse.swt.widgets.Text.sendKeyEvent(Text.java:1427)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1099)
at org.eclipse.swt.widgets.Widget.wmChar(Widget.java:1508)
at org.eclipse.swt.widgets.Control.WM_CHAR(Control.java:4268)
at org.eclipse.swt.widgets.Text.WM_CHAR(Text.java:2175)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4160)
at org.eclipse.swt.widgets.Text.windowProc(Text.java:2170)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4873)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2459)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3655)
at com.hezhi.kylwsp.ui.main.LoginDialog.open(LoginDialog.java:83)
at com.hezhi.kylwsp.ui.main.Login.main(Login.java:308)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.exe4j.runtime.LauncherEngine.launch(Unknown Source)
at com.exe4j.runtime.WinLauncher.main(Unknown Source)

封装的方法:
public static String doPost(String parm,String url) {
    String result = "";
    PrintWriter out = null;
    BufferedReader in = null;
    try {

        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        URLConnection conn = realUrl.openConnection();
        // 设置通用的请求属性
        //conn.setRequestMethod("POST");  
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "application/json"); 
        conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 发送POST请求必须设置如下两行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 获取URLConnection对象对应的输出流
        out = new PrintWriter(conn.getOutputStream());
        // 发送请求参数
        out.print(parm);
        // flush输出流的缓冲
        out.flush();
        // 定义BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));

// in = new OutputStreamWriter(con.getOutputStream());
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
System.out.println(result);
return result;
}

展开全部

  • 写回答

5条回答 默认 最新

  • 880露露088 2017-12-11 06:42
    关注

    要打包成war包。不能是jar包。

    评论
  • Mr.Monkey 2017-12-11 06:50
    关注

    web项目是打包成war,不是jar。

    评论
  • COCO_AS 2017-12-11 06:55
    关注

    问题应该与jar包不jar包无关, 应该是你的访问参数不对, 返回400是因为请求不符合服务器约定, 找接口开发人员要文档吧

    评论
  • JPF1024 2017-12-11 21:34
    关注

    400是参数不对,你用日志记录一下,在eclipse和jar包运行的时候发送的参数是不是不一样

    评论
  • 奥利弗嗷 2017-12-15 07:24
    关注

    根据你项目的打包类型来进行打包

    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部