相关疑难解决方法(0)

为什么我的C#客户端,POST到我的WCF REST服务,返回(400)错误请求?

我正在尝试向我编写的简单WCF服务发送POST请求,但我一直收到400错误请求.我正在尝试将JSON数据发送到服务.谁能发现我做错了什么?:-)

这是我的服务界面:

public interface Itestservice
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "/create",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    String Create(TestData testData);
}
Run Code Online (Sandbox Code Playgroud)

实施:

public class testservice: Itestservice
{
    public String Create(TestData testData)
    {
        return "Hello, your test data is " + testData.SomeData;
    }
}
Run Code Online (Sandbox Code Playgroud)

DataContract:

[DataContract]
public class TestData
{
    [DataMember]
    public String SomeData { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

最后我的客户端代码:

private static void TestCreatePost()
{
    Console.WriteLine("testservice.svc/create POST:");
    Console.WriteLine("-----------------------");

    Uri address = new Uri("http://localhost:" + PORT + "/testweb/testservice.svc/create");

    // …
Run Code Online (Sandbox Code Playgroud)

c# rest wcf post

18
推荐指数
1
解决办法
3万
查看次数

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

无法让我的JQuery POST被WCF服务接受.这是来自javascript的POST:

function jqueryPost() {
    var url = "/LoggingTest";
    $.post(url, { message: "test message" });
}
Run Code Online (Sandbox Code Playgroud)

这是我通过接口接受POST的方式:

[OperationContract]
[WebInvoke(Method = "POST",
           UriTemplate = "/LoggingTest",
           BodyStyle = WebMessageBodyStyle.Bare)]
void LoggingTest(string message);
Run Code Online (Sandbox Code Playgroud)

并实施:

public void LoggingTest(string message)
{
    log.Debug(message, null);
}
Run Code Online (Sandbox Code Playgroud)

当我调用函数jqueryPost时,我在Web检查器中看到了400 Bad Request的HTTP响应.不确定如何让POST请求工作.

(在7/1上添加)
@James,这是web检查器的输出:

http:// localhost:4252/LoggingTest HTTP信息
请求方法:POST
状态码:400错误请求
请求标头
接受:/
Cache-Control:max-age = 0
内容类型:application/x-www-form-urlencoded
原产地:http:// localhost:4252
Referer:http:// localhost:4252 /
User-Agent:Mozilla/5.0(Windows; U; Windows NT 5.1; C - )AppleWebKit/532.4(KHTML,如Gecko)Qt/4.6.2 Safari/532.4
X-Requested-With:XMLHttpRequest
表单数据
消息:测试消息
响应标头
内容长度:1165
内容类型:text/html …

c# wcf jquery post httpwebrequest

7
推荐指数
1
解决办法
2万
查看次数

将大型xml上载到WCF REST服务 - > 400 Bad request

我正在尝试将大型xml文件上传到REST服务...我已经尝试了在谷歌上的stackoverflow上指定的几乎所有方法,但我仍然无法找到我出错的地方....我无法上传大于64 kb的文件!..

我已经指定了maxRequestLength:

<httpRuntime maxRequestLength="65536"/>
Run Code Online (Sandbox Code Playgroud)

我的绑定配置如下:

<bindings>
  <webHttpBinding>
    <binding name="RESTBinding" maxBufferSize="67108864" maxReceivedMessageSize="67108864" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
    </binding>   
  </webHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

在我的C#客户端,我正在做以下事情:

WebRequest request = HttpWebRequest.Create(@"http://localhost.:2381/RepositoryServices.svc/deviceprofile/AddDdxml");

        request.Credentials = new NetworkCredential("blah", "blah");
        request.Method = "POST";
        request.ContentType = "application/xml";
        request.ContentLength = byteArray.LongLength;


        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteArray, 0, byteArray.Length);
        }
Run Code Online (Sandbox Code Playgroud)

客户端没有特殊配置......

我试过fiddler ...客户端正在发送一个正确的请求...但服务器立即响应400 ..

c# rest upload wcf

5
推荐指数
1
解决办法
4689
查看次数

标签 统计

c# ×3

wcf ×3

post ×2

rest ×2

httpwebrequest ×1

jquery ×1

upload ×1