jQuery를 통해 호출 할 수있는 WCF 서비스가 있지만 HttpWebRequest를 통해 호출 할 수 없습니다. 이전과 똑같은 HttpWebRequest 설정을 사용하여 ASMX 서비스를 호출 할 수 있었지만 이것이 WCF 서비스를 호출하는 첫 번째 시도입니다. jQuery와 C# HttpWebRequest 코드를 제공 했으므로 분명히 잘못된 것을 알 수 있습니다. HTTP 오류 400 받기 HttpWebRequest를 통해 WCF 서비스 호출
이
는, 마지막 줄string url = "http://www.site.com/Service/Webapi.svc/GetParts";
string parameters = "{part: 'ABCDE'}";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentLength = 0;
req.ContentType = "application/json; charset=utf-8";
if (!string.IsNullOrEmpty(parameters))
{
byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
req.ContentLength = byteArray.Length;
Stream dataStream = req.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
이 400
내이 jQuery 코드를 여기에$.ajax({
url: '/Service/Webapi.svc/GetParts',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
data: JSON.stringify({ part: request.term }),
success: function (data) {
// Success
}
});
를 작동 오류를 내 깨진 C# 코드 던지고있다 나의 상대의 web.config 설정입니다
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<enableWebScript/>
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service name="Webapi">
<endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding" contract="IWebapi"/>
</service>
</services>
</system.serviceModel>
내 서비스 인터페이스는 "방법"을 "POST"로 변경하려고했지만 광고를 만들지 않았습니다. ifference. 여기
[ServiceContract]
public interface IWebapi
{
[OperationContract]
[WebInvoke(
Method = "*",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)
]
string[] GetParts(string part);
}
이 훨씬 이전에 명백한에서했지만, 난 문제를 발견, 내 서비스 구현
public string[] GetParts(string part)
{
return new string[] {"ABC", "BCD", "CDE"};
}