繁体   English   中英

WCF服务:400错误的请求?

[英]WCF service:400 bad request?

我从7天开始使用wcf。我创建了一个Wcf服务。 但是工作正常,并通过添加服务参考在Visual Studio中获得了响应。 但是我的问题是,每当它集成在android / windows中时,这些人都不会得到响应。 他们得到400(错误请求)。 请任何人帮助我如何获得适当的答复。 (我想作为JSON响应)这是我下面的代码。 先感谢您。

在界面中

public interface ISampleService1
    {
        [OperationContract]

         [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "getdata",BodyStyle = WebMessageBodyStyle.Bare)]
         string getdata();
}

在班上

 public  string getdata()
        {
            try
            {


                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con2"].ConnectionString);
                SqlCommand comm = new SqlCommand("select CircleID, CircleName from Circle", con);
                con.Open();
                comm.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter(comm);
                // DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);

                JavaScriptSerializer JSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                List<Dictionary<string, object>> DtRows = new List<Dictionary<string, object>>();
                Dictionary<string, object> newrow = null;

                //Code to loop each row in the datatable and add it to the dictionary object
                foreach (DataRow drow in dt.Rows)
                {
                    newrow = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        newrow.Add(col.ColumnName.Trim(), drow[col]);
                    }
                    DtRows.Add(newrow);
                }
                con.Close();


                return JSSerializer.Serialize(DtRows);
            }
            catch (Exception ex)
            {

                List<String> Parameters = new List<String>();
                Parameters.Add("getRechargeCircleList");

             //   SendErrorMail(ex.ToString(), "getRechargeCircleList", Parameters);
               // Context.Response.ContentType = "application/json; charset=utf-8";
               // Context.Response.Write(@" {""Status"":""false"",""Message"" : ""Oops! Something went Wrong""}");
               // return;
            }
            return "false";
        }

web.config文件:

<?xml version="1.0"?>
<configuration>
  <appSettings/>




  </connectionStrings>


  <system.web>

    <httpRuntime maxRequestLength="32768"/>
    <compilation debug="true" targetFramework="4.0"/>
     <customErrors mode="Off"/>
    <!--
        The <authentication> section enables configuration 
        of the security authentication mode used by 
        ASP.NET to identify an incoming user. 
    -->
   <authentication mode="Windows"/>
    <!--
        The <customErrors> section enables configuration 
        of what to do if/when an unhandled error occurs 
        during the execution of a request. Specifically, 
        it enables developers to configure html error pages 
        to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
         <error statusCode="403" redirect="NoAccess.htm" />
         <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
    -->
    <pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID"/>
  </system.web>
  <!-- 
      The system.webServer section is required for running ASP.NET AJAX under Internet
      Information Services 7.0.  It is not necessary for previous version of IIS.
  -->
  <system.webServer>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <system.serviceModel>

    <bindings>
    <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="4194304" 
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
            <readerQuotas maxDepth="32" 
                 maxArrayLength="200000000"
                 maxStringContentLength="200000000"/>
        </binding>
    </basicHttpBinding>
</bindings>
    <services>
      <service behaviorConfiguration="SampleWcfproject.Service1Behavior" name="SampleWcfproject.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="SampleWcfproject.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service behaviorConfiguration="SampleWcfproject.SampleService1Behavior" name="SampleWcfproject.SampleService1">
        <endpoint address="" binding="basicHttpBinding" contract="SampleWcfproject.ISampleService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
       <endpointBehaviors>
            <behavior name="web">
                <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
            </behavior>
        </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="SampleWcfproject.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="SampleWcfproject.SampleService1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
</configuration>

通过使用Web参考项目,我在Visual Studio中得到了以下建议。 但是在Android中出现类似400的错误。

[{“” CircleID“”:1,“” CircleName“”:“”“安得拉邦”“},{”“ CircleID”“:4,”“ CircleName”“:”“ Assam”“},{”“ CircleID “”:5,“” CircleName“”:“” Bihar“”},{“” CircleID“”:6,“” CircleName“”:“” Chennai“”},{“” CircleID“”:7,“ “ CircleName”“:”“ Delhi”“},{”“ CircleID”“:8,”“ CircleName”“:”“ Gujarat”“},{”“ CircleID”“:9,”“ CircleName”“:” “ Himachal Pradesh”“},{”“ CircleID”“:10,”“ CircleName”“:”“ Haryana”“},{”“ CircleID”“:11,”“ CircleName”“:”“ Jammu \\ u0026克什米尔“”},{“” CircleID“”:12,“” CircleName“”:“”“喀拉拉”“},{” CircleID“”:13,“” CircleName“”:“” Karnataka“”},{“ “ CircleID”“:14,”“ CircleName”“:”“加尔各答”“},{”“ CircleID”“:15,”“ CircleName”“:”“马哈拉施特拉邦”“},{”“ CircleID”“:16 ,“” CircleName“”:“”中央邦“”},{“” CircleID“”:17,“” CircleName“”:“”“孟买”“},{”“ CircleID”“:18,”“ CircleName” “:”“东北”“},{”“ CircleID”“:19,”“ CircleName”“:”“ Orissa”“},{”“ CircleID”“:20,”“ CircleName”“:”“旁遮普语“”},{“” CircleID“”:21,“” CircleName“”:“” Rajasthan“”},{“” CircleID“”:22,“” CircleName“}]”));

这是我的网址http://xyzmnmkljj.in/SampleService1.svc/getdata

但它给出了400个错误的请求。

对web.comfig文件的任何修改,请帮助我

如果可以,请检查您的IP是否可以被那些机器访问,然后您需要授予该用户访问服务的权限。 在这种情况下,您需要将IIS设置更改为提供程序中的NTLM。

blogs.msdn.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM