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

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 14:42
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题