错误请求 - 作为Android消耗WCF restful Web服务的结果的无效主机名

时间:2013-09-26 11:11:55

标签: android json wcf web-services rest

我正在尝试从android调用我的wcf webservices但是在阅读响应流时我得到的只是这个

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
\n<HTML>
<HEAD><TITLE>Bad Request</TITLE>
\n<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
\n<BODY><h2>Bad Request - Invalid Hostname</h2>
\n<hr><p>HTTP Error 400. The request hostname is invalid.</p>
\n</BODY></HTML>\n

这是我的客户方

String result = "";
    String url ="http://10.0.2.2:81/WScom.svc/GetUserList";
    InputStream is = null;
    try {

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(url);


        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Content-Type", "application/json");


        HttpResponse response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();

        is = entity.getContent();

    } catch (Exception e) {
        result = e.toString();
    }

    try {
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();

    reader = new BufferedReader(new InputStreamReader(is));
    String line = "";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
        //System.out.println(line);
    }

    result = sb.toString();

        is.close();
    } 
    catch (Exception e) {

        result = e.toString();
    }
    return (result);
}       

虽然当我在浏览器中粘贴链接时(当然因为android模拟器要求而使用localhost而不是10.0.2.2)它会显示一个完美的json结果!所以有什么帮助吗?

0 个答案:

没有答案