.net rest service with JSON string and consumed with java client

718 views Asked by At

I want to consume the .net JSON string web services in java. I am getting bad request error always but through SOAP UI i am getting the response. Any one suggest me how to consume the .net rest services in java.

"{"\DocumentId\":"\29292\","\Note\":"\jaasBook\"}"

String uri = "http://example.com/service.asmx/GetInfo";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();    
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Accept", "application/json");            
connection.setChunkedStreamingMode(0);
connection.connect();   
byte[] parameters = {"\DocumentId\":"\29292\","\Note\":"\jaasBook\"}".getBytes("UTF-8");                
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.write(parameters);
os.close();         
InputStream response; 
if(connection.getResponseCode() == 200){response = connection.getInputStream();}
else{response = connection.getErrorStream();}
InputStreamReader isr = new InputStreamReader(response);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(isr);
String read = br.readLine();
while(read != null){
    sb.append(read);
    read = br.readLine();
}
3

There are 3 answers

0
bdunn On

At first glance it looks like you are improperly escaping the JSON string:

byte[] parameters = "{\"DocumentId\":\"29292\",\"Note\":\"jaasBook\"}".getBytes("UTF-8");

Note that the backslash comes before each double-quote you need to escape.

4
Safwan Hijazi On

use this code to send and recieve response

public static String makeHttpCall(String strRequest)
            throws ServiceException {

        try {
            String response = null;
            URL url = new URL(
                    "http://example.com/service.asmx/GetInfo");
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            DataOutputStream dstream = new DataOutputStream(
                    connection.getOutputStream());
             dstream.writeBytes(strRequest);
             dstream.flush();
             dstream.close();
            StringBuffer sb = new StringBuffer();
            InputStreamReader content = new InputStreamReader(
                    connection.getInputStream());
            for (int i = 0; i != -1; i = content.read()) {
                sb.append((char) i);
            }
            response = sb.toString().substring(1, sb.toString().length());
            System.out.println("Response:");
            System.out.println(response);
            return response;

        } catch (MalformedURLException e) {
            throw new ServiceException("MalformedURLException", e);
        } catch (ProtocolException e) {
            throw new ServiceException("ProtocolException", e);
        } catch (IOException e) {
            throw new ServiceException("IOException", e);
        } 

    }

then use Gson lib to parse Json to object

0
ESB For Next Generation On

Finally i consumed the .net json web service with java client. Thanks every one for the support.

public class JsonRestEx {
public static void main(String[] args) {
    try {
        StringBuilder stringBuilder = new StringBuilder("\"");
        stringBuilder.append("{");
        stringBuilder.append("\\");
        stringBuilder.append("\"");
        stringBuilder.append("DocumentDefinition");
        stringBuilder.append("\\");
        stringBuilder.append("\"");
        stringBuilder.append(":");
        stringBuilder.append("\\");
        stringBuilder.append("\"");
        stringBuilder.append("google.com||CC/APP/44||Loan Application file||CAS3333||3333||Loan|| Loan||AC222||LN8888");
        stringBuilder.append("\\");
        stringBuilder.append("\"");         
        stringBuilder.append(",");          
        stringBuilder.append("\\");
        stringBuilder.append("\"");
        stringBuilder.append("correlationId");
        stringBuilder.append("\\");
        stringBuilder.append("\"");
        stringBuilder.append(":");
        stringBuilder.append("\\");
        stringBuilder.append("\"");
        stringBuilder.append("43754309657043769854");
        stringBuilder.append("\\");
        stringBuilder.append("\"");         
        stringBuilder.append("}");
        stringBuilder.append("\"");         
        System.out.println("FINAL STR:  "+ stringBuilder.toString());       
        String urlString="http://12.2.2.0/RS/dfs.svc/sfd";          
         HttpPost request = new HttpPost(urlString);
         StringEntity entity = new StringEntity(stringBuilder.toString(), "UTF-8");
         entity.setContentType("application/json;charset=UTF-8");
         entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
         request.setEntity(entity);
         DefaultHttpClient httpClient = new DefaultHttpClient();
         HttpResponse response = httpClient.execute(request); 
         int statusCode = response.getStatusLine().getStatusCode();
         System.out.println("statusCode:  "+ statusCode);            
         BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));           
            String line = "";            
            while ((line = rd.readLine()) != null) {             
                System.out.println(line);            
            }        
    }catch (Exception e) {            
        e.printStackTrace();            
    }
}

}