.net休息服务与JSON字符串,并与Java客户端一起使用

时间:2015-06-08 20:28:59

标签: java .net web-services rest

我想在java中使用.net JSON字符串Web服务。我总是得到错误的请求错误,但通过SOAP UI,我得到了响应。任何人都建议我如何在java中使用.net rest服务。

" {" \ DocumentId \":" \ 29292 \"" \注意\":" \ 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 个答案:

答案 0 :(得分:1)

乍一看,您似乎错误地转义了JSON字符串:

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

请注意,反斜杠出现在您需要转义的每个双引号之前。

答案 1 :(得分:0)

使用此代码发送和接收回复

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

    }

然后使用Gson lib将Json解析为对象

答案 2 :(得分:0)

最后我用java客户端使用了.net json web服务。感谢每一位人士的支持。

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

}