通过WebClient使用WCF POST进行400错误请求HTTP响应

时间:2018-06-15 14:21:47

标签: c# .net wcf post service

这是我的POST方法

   [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/InspectionSave")]
    public string InspectionSave(Inspections _Inspections)
    {
        try
        {
            IList<InspectionSlings> _InspectionSlings = new List<InspectionSlings>();
            _InspectionSlings = _Inspections.InspSlings;
            DataTable _dt = new DataTable();
            _dt.Columns.Add("MeasuredDiameter");
            _dt.Columns.Add("Kinks");
            _dt.Columns.Add("Crushed");
            _dt.Columns.Add("Birdcage");
            _dt.Columns.Add("Brokenwire");
            _dt.Columns.Add("HeatDamage");
            _dt.Columns.Add("EndAttachmentFitting");
            _dt.Columns.Add("EndAttachmentBrokenWires");
            _dt.Columns.Add("HookCondition");
            _dt.Columns.Add("SlingIsServiceable");
            _dt.Columns.Add("SlingIsRejected");
            _dt.Columns.Add("Comments");

            foreach (InspectionSlings inspectionSling in _InspectionSlings)
            {
                DataRow dr = _dt.NewRow();
                dr[0] = inspectionSling.MeasuredDiameter;
                dr[1] = inspectionSling.Kinks;
                dr[2] = inspectionSling.Crushed;
                dr[3] = inspectionSling.Birdcage;
                dr[4] = inspectionSling.Brokenwire;
                dr[5] = inspectionSling.HeatDamage;
                dr[6] = inspectionSling.EndAttachmentFitting;
                dr[7] = inspectionSling.EndAttachmentBrokenWires;
                dr[8] = inspectionSling.HookCondition;
                dr[9] = inspectionSling.SlingIsServiceable;
                dr[10] = inspectionSling.SlingIsRejected;
                dr[11] = inspectionSling.Comments;
                _dt.Rows.Add(dr);
            }

            string result = LoginDB.InspectionSave(_dt, _Inspections);
            return result;
        }
        catch(Exception ex)
        {
            string result = ex.ToString();
            return result;
        }
    }

Web.Config

 <system.serviceModel>
<services>
  <service behaviorConfiguration="Default" name="WireInspectionService.WireInspectionService">
    <endpoint address="" binding="webHttpBinding" contract="WireInspectionService.IWireInspectionService" behaviorConfiguration="web"  bindingConfiguration="b1" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:22456/WireInspectionService" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="b1" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647"
                    maxStringContentLength="2147483647"
                    maxArrayLength="2147483647"
                    maxBytesPerRead="2147483647"
                    maxNameTableCharCount="2147483647" />
    </binding>
  </webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

WebClient调用

 WebClient _webClient = new WebClient();
            _webClient.Headers["Content-type"] = "text/json";

            Inspections _Inspections = new Inspections();
            _Inspections.InspectionDate = DateTime.Now;
            _Inspections.CreatedBy = Guid.NewGuid();
            _Inspections.InspectionId = Guid.NewGuid();
            _Inspections.InspectorId = Guid.NewGuid();
            _Inspections.LocationName = "Houston";
            _Inspections.SupervisorId = Guid.NewGuid();
            _Inspections.InspSlings = new List<InspectionSlings>();

            _Inspections.InspSlings.Add(new InspectionSlings
            {
                MeasuredDiameter = "1",
                Kinks = "2",
                Crushed = "3",
                Birdcage = "4",
                Brokenwire = "5",
                HeatDamage = "6",
                EndAttachmentFitting = "7",
                EndAttachmentBrokenWires = "8",
                HookCondition = "9",
                SlingIsServiceable = true,
                SlingIsRejected = true,
                Comments = "1"
            });

            string sUserData = JsonConvert.SerializeObject(_Inspections);

            string Url = " http://localhost:60220/";
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer serializerToUplaod = new DataContractJsonSerializer(typeof(Inspections));
            serializerToUplaod.WriteObject(ms, _Inspections);

            WebClient Proxy1 = new WebClient();
          //  string UserID = _webClient.UploadString(Url + "/WireInspectionService.svc/InspectionSave", "POST", sUserData);


            var data = Proxy1.UploadData(Url + "/WireInspectionService.svc/InspectionSave", "POST", ms.ToArray());

            var stream = new MemoryStream(data);

            var obj = new DataContractJsonSerializer(typeof(object));
            var retval = obj.ReadObject(stream) as string;

这是我的Json结果

{
    "InspectionId": "55736d1f-604f-4457-bb45-f9b2dfacf5a9",
    "InspectionDate": "2018-06-15T19:06:18.4400844+05:30",
    "InspectorId": "ff9a322f-18db-4aad-ba71-2297c0731c0e",
    "SupervisorId": "6086775a-eee3-4029-949a-a98a4bda56cd",
    "LocationName": "Houston",
    "CreatedBy": "5cb04cf6-d747-46e2-8631-b2ae4604e317",
    "InspSlings": [{
        "MeasuredDiameter": "1",
        "Kinks": "2",
        "Crushed": "3",
        "Birdcage": "4",
        "Brokenwire": "5",
        "HeatDamage": "6",
        "EndAttachmentFitting": "7",
        "EndAttachmentBrokenWires": "8",
        "HookCondition": "9",
        "SlingIsServiceable": true,
        "SlingIsRejected": true,
        "Comments": "1"
    }]
}

1 个答案:

答案 0 :(得分:0)

看起来您要发布的网址可能包含额外的'/'。

string Url = " http://localhost:60220/";
var data = Proxy1.UploadData(Url + "/WireInspectionService.svc/InspectionSave", "POST", ms.ToArray());

Url以“/”结尾,然后附加“/WireInspectionService.svc/InspectionSave”

因此,生成的网址如下所示:“http://localhost:60220//WireInspectionService.svc/InspectionSave

此外,Url还有一个领先的空间。希望这有帮助!