使用Fiddler调试Wcf Rest

1 使用Fiddler调试Wcf Rest

1.1 怎样使用断点调试

1.1.1 运行项目

1.1.2 调试à附加进程àFiddler.exe(勾选显示所有用户的进程)

1.2 Get方法

1.2.1 Fiddlerà选择Composerà选择Get并输入服务地址
image

1.3 Post方法

1.3.1 Fiddlerà选择Composerà选择Post并输入服务地址
image

1.3.2 在Request Body写入传递参数
clip_image006

1.3.3 在Request Headers写入参数头,如果要传递的是xml类型参数,需加入Content-Type: application/xml,否则服务无法使用
clip_image008

2 使用流的方法调用方法(Post)

2.1 源代码

private byte[] ToByteArray(Stream stream)

{

byte[] buffer = new byte[32768];

using (MemoryStream ms = new MemoryStream())

{

while (true)

{

int read = stream.Read(buffer, 0, buffer.Length);

if (read <= 0)

return ms.ToArray();

ms.Write(buffer, 0, read);

}

}

}

[WebInvoke(UriTemplate = "/CreateStream?message={xmlInput}", Method = "POST")]

public Stream CreateStream(string xml, Stream body)

{

byte[] bodyData = ToByteArray(body);

string bodyContent = Encoding.UTF8.GetString(bodyData);

XElement ele = XElement.Parse(bodyContent);

WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";

XDocument doc = new XDocument(

new XDeclaration("1.0", "utf-8", "yes"),

new XElement("Returns",

new XElement("Status", 1),

new XElement("Msg", "登陆成功的消息"),

new XElement("Id", ele.Element("Id").Value),

new XElement("StringValue", ele.Element("StringValue").Value))

);

xml = doc.ToString();

xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + xml.Replace("\r\n", "").Replace(" ", "");

byte[] buffer = System.Text.Encoding.UTF8.GetBytes(xml);

Stream st = new MemoryStream(buffer);

st.Flush();

st.Position = 0;

return st;

}
2.2 Fiddler测试方法(特别注意的是,request headers不要加入Content-Type: application/xml,否则会返回内部错误,也无法触发方法断点)
clip_image010

 

3 可供参看的网址

3.1 http://www.britishdeveloper.co.uk/2011/01/how-to-post-rest-fiddler.html

posted @ 2012-03-26 20:24    阅读(1551)  评论(0编辑  收藏  举报