开发者

How to fake Http post?

I am using asp.net开发者_开发知识库 mvc and I want to fake a http post to see what would happen. Is there any software that I can use?


I believe that Fiddler allows you to do this, plus a whole lot more.

I only use it for reviewing what's going to/from the server when dealing with AJAX induced headaches, but I'm fairly sure you can use it to re-issue HTTP requests both as they were originally and modified, which should fit the bill for you.


string var1 = "Foo";
string var2 = "Bar";

ASCIIEncoding encoding = new ASCIIEncoding();
string post = "var1=" + var1 + "&var2=" + var2;
byte[] bites = encoding.GetBytes(post);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://Url/PageToPostTo.aspx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bites.Length;
Stream s = request.GetRequestStream();
s.Write(bites, 0, bites.Length);
s.Close();


I like TamperData, a Firefox addon.


Here is some javascript for you:

function makeRequest(message,url,responseFunction){
var http_request;
    if (window.XMLHttpRequest){ // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
        // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
    }
}
else if (window.ActiveXObject){ // IE
    try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e){
        try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
}

http_request.onreadystatechange = responseFunction;
    http_request.open("POST", url);
http_request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
http_request.send(message);
}


Charles has the ability to capture any http requests and responses and allows you to save sessions and edit/repeat them with easy. Worth a try and see if it's to your liking.


The below open source project allows you to fake external web services in your acceptance tests.

It supports the common HTTP verbs GET, POST, DELETE & PUT;

http://www.nuget.org/packages/boomerang/ https://github.com/garfieldmoore/Boomerang

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜