首页 新闻 搜索 专区 学院

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

0
悬赏园豆:50 [已解决问题] 解决于 2018-04-19 19:17

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)

封装的方法:

个人认为是错误引用jar,下面个人认为是问题的关键点:

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

import java.net.URL;
import java.net.URLConnection;


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;
}

花开半夏雨的主页 花开半夏雨 | 初学一级 | 园豆:6
提问于:2017-12-11 20:36
< >
分享
最佳答案
0

很有可能空格被转变成20%,不是url错误的话,重打下jar包试试

收获园豆:50
DanBrown | 小虾三级 |园豆:1513 | 2017-12-19 08:38

重新封装的方法,没问题了,具体问题也说不清出在哪儿

花开半夏雨 | 园豆:6 (初学一级) | 2018-01-21 21:54
其他回答(1)
0

请求的具体url是多少  看看有没有空格

ycyzharry | 园豆:24314 (高人七级) | 2017-12-12 09:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册