Android应用程序调用.NET Web服务

时间:2014-03-06 07:32:56

标签: android .net web-services http-post

我已经创建了我的.NET Web服务,我正在尝试从我的Android应用程序使用XMLPullParser获取数据,但我得到了HTTP/1.1 500 Internal Server Error

这是一个示例HttpRequest:

POST /CompanyService.asmx/AddUser HTTP/1.1
Host: 192.168.141.1
Content-Type: application/x-www-form-urlencoded
Content-Length: length

UserName=string&Password=string&PhoneNumber=string&EmailID=string&Position=string&Address=string

我的android代码是:

 HttpPost httppost = new HttpPost("http://192.168.141.1/CompanyService.asmx/AddUser");

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
                nameValuePairs.add(new BasicNameValuePair("UserName", "aaa"));
                nameValuePairs.add(new BasicNameValuePair("Password", "a"));
                nameValuePairs.add(new BasicNameValuePair("PhoneNumber", "a"));
                nameValuePairs.add(new BasicNameValuePair("EmailID", "a"));
                nameValuePairs.add(new BasicNameValuePair("Position", "a"));
                nameValuePairs.add(new BasicNameValuePair("Address", "a"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                e.printStackTrace();

            } catch (IOException e) {
              e.printStackTrace();
            }

我在这里做错了什么?

编辑: 日志文件包含:

2014-03-06 07:24:19 192.168.141.1 POST /CompanyService.asmx/AddUser - 80 - 192.168.141.101 Apache-HttpClient/UNAVAILABLE+(java+1.4) - 500 0 0 10

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我没有在web.config中添加协议。这是代码

  <webServices>

   <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>

  </webServices>