使用Java发送和接收JSON



我试图使用java GUI发送和接收JSON,但是,我使用http://localhost:8080/clienteWebService/test-resbeans.html时不断收到一些错误(如500405403(。我可以正常使用它们,发送和接收任何问题的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");

}

他们用网络服务发送代码

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:

问题出在Stringurl中,这是我在正确链接中发出请求所需的代码和格式。

public void Inserir () throws Exception {
InterfaceConsu2 IFC2 = new InterfaceConsu2();
ConsumirWS2 http = new ConsumirWS2();
Gson g = new GsonBuilder().setDateFormat("dd-MM-yyyy").create();
Cliente2 u = new Cliente2();
Type ClienteType = new TypeToken<Cliente2>() {
}.getType();


u.setCad_pes_nome(IFC2.jTextFieldNOME.getText());
u.setCad_pes_cpf(IFC2.jFormattedTextFieldCPF.getText());
u.setCad_pes_apelido(IFC2.jTextFieldAPELIDO.getText());
SimpleDateFormat formatter  = new SimpleDateFormat ("dd-MM-yyyy");
java.util.Date utilDate = null;
try {
utilDate = formatter.parse(IFC2.jFormattedTextFieldDATA.getText());
} catch (ParseException ex) {
Logger.getLogger(InterfaceConsu2.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/CadastroCliente/Cliente/inserir";
System.out.println("63 consumir");
System.out.println(json);
http.sendPost(url, json, "POST");
}

我认为代码中几乎没有什么小的变化,但这适用于零错误

此外,由于需要向Web服务发送参数,下面是我自己做的另一个例子,我想知道如何做到这一点,任何其他建议都欢迎

u.setCad_pes_cpf(IFC2.jFormattedTextFieldCPF.getText());
String url = "http://localhost:8080/clienteWebService/webresources/CadastroCliente/Cliente/get/" +u.getCad_pes_cpf();

在这个例子中,在此之后,我调用一个GET方法

相关内容

  • 没有找到相关文章

最新更新