我尝试使用java GUI发送和接收JSON,但是,我一直收到一些错误(如,500,405,403),我使用http://localhost:8080/clienteWebService/test-resbeans.html
他们我可以正常使用,发送和接收JSON与任何问题。
对于这个程序,我试图做我也得到这个错误:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8080/ClienteWebService/webresources/Cadastro%20Cliente/Cliente/inserir
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939)
at java.security.AccessController.doPrivileged(Native Method)
我不知道哪里出错了,我试图使用http://localhost:8080/clienteWebService/webresources/Cadastro%20Cliente/Cliente/inserir
直接发出POST
请求
并且我接收JSON,但是,如果我用jTextField获取信息,则不能正常运行或者根本不能运行。
这是代码:
public static void Inserir () throws Exception {
ConsumirWs1 http = new ConsumirWs1();
Gson g = new Gson();
Cliente u = new Cliente();
Type ClienteType = new TypeToken<Cliente>() {
}.getType();
u.setCad_pes_nome(InterfaceConsu.jTextFieldNOME.getText());
u.setCad_pes_cpf(InterfaceConsu.jFormattedTextFieldCPF.getText());
u.setCad_pes_apelido(InterfaceConsu.jTextFieldAPELIDO.getText());
SimpleDateFormat formatter = new SimpleDateFormat ("dd-MM-yyyy");
java.util.Date utilDate = null;
try {
utilDate = formatter.parse(jFormattedTextFieldDATA.getText());
} catch (ParseException ex) {
Logger.getLogger(InterfaceConsu.class.getName()).log(Level.SEVERE, null, ex);
}
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
u.setCad_pes_data(sqlDate);
String json = g.toJson(u, ClienteType);
String url = "http://localhost:8080/clienteWebService/webresources/Cadastro%20Cliente/Cliente/inserir?";
http.sendPost(url, json, "POST");
}
然后将代码与Web服务一起发送
private void sendPost(String url, String urlParameters, String method) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod(method);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.writeBytes(urlParameters);
wr.flush();
}
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
StringBuffer response;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
//print result
System.out.println(response.toString());
}
如果我尝试GET,我会收到错误java.net.ProtocolException: Invalid HTTP method:
1条答案
按热度按时间az31mfrm1#
问题是在字符串URL,这里的代码和格式,我需要在正确的链接请求.
我认为在代码中有一些小的变化,但这是零错误的工作
另外,需要使用将参数发送到Web服务,这是我自己制作的另一个示例,了解如何执行此操作,欢迎提供任何其他建议
在这个例子中,在这之后我调用一个GET方法