0

私が書いているいくつかのテストの文字列として、HttpPOSTリクエストとおそらくいくつかのGETリクエストを作成する必要があります。現在、私のテストでは、StringBuilderと、フィドルから引き出されたハードコードされたPOSTリクエストを使用して次のようにビルドしています。

var builder = new StringBuilder();
builder.Append("POST https://some.web.pg HTTP/1.1\r\n");
builder.Append("Content-Type: application/x-www-form-urlencoded\r\n");
builder.Append("Referer: https://some.referer.com\r\n");
builder.Append("Accept-Language: en-us\r\n");
builder.Append("Accept-Encoding: gzip, deflate\r\n");
builder.Append("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\r\n");
builder.Append("Host: login.yahoo.com\r\n");
//    ... other header info
builder.Append("\r\n");
builder.Append("post body......\r\n");
var postData = builder.ToString();

これはすぐに私のテストを乱雑にし、これらのPOSTリクエストを作成するためのよりクリーンな方法を好むでしょう。私はHttpWebRequestクラスを調べていて、おそらくそれが私のためにこれらを作成できることを望んでいます。私は、意味の背後に、私が何らかの形で作成しようとしているこの正確な要求を構築するための何らかの方法が必要であると考えました。しかし残念ながら、GetRequestStreamは書き込み可能な唯一のストリームです。

HttpWebRequestが生成する(そして文字列に変更する)リクエストストリームを読み取る方法はありますか?または、これらのPOSTリクエストを生成する方法についてのアイデアでさえも可能です。

4

3 に答える 3

0

Get リクエストを作成するための msdn サンプルは次のとおりです。

using System;

System.Net を使用。System.IO を使用します。

namespace MakeAGETRequest_charp { /// /// Class1 の概要説明。/// class Class1 { static void Main(string[] args) { string sURL; sURL = " http://www.microsoft.com ";

        WebRequest wrGETURL;
        wrGETURL = WebRequest.Create(sURL);

        WebProxy myProxy = new WebProxy("myproxy",80);
        myProxy.BypassProxyOnLocal = true;

        wrGETURL.Proxy = WebProxy.GetDefaultProxy();

        Stream objStream;
        objStream = wrGETURL.GetResponse().GetResponseStream();

        StreamReader objReader = new StreamReader(objStream);

        string sLine = "";
        int i = 0;

        while (sLine!=null)
        {
            i++;
            sLine = objReader.ReadLine();
            if (sLine!=null)
                Console.WriteLine("{0}:{1}",i,sLine);
        }
        Console.ReadLine();
    }
}

ここでは、post リクエストの場合 (post を使用したHTTP リクエストから)

    HttpWebRequest httpWReq =
    (HttpWebRequest)WebRequest.Create("http:\\domain.com\page.asp");

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=user";
postData += "&password=pass";
byte[] data = encoding.GetBytes(postData);

httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;

using (Stream newStream = httpWReq.GetRequestStream())
{
    newStream.Write(data,0,data.Length);
}
于 2012-08-04T01:51:33.390 に答える
0
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(yoururllink);
    var c = HttpContext.Current;

    //Set values for the request back
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
    string strRequest = Encoding.ASCII.GetString(param);
    string strResponse_copy = strRequest;  //Save a copy of the initial info sent from your url link
    strRequest += "&cmd=_notify-validate";
    req.ContentLength = strRequest.Length;

    //for proxy
    //WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
    //req.Proxy = proxy;

    //Send the request to PayPal and get the response
    StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
    streamOut.Write(strRequest);
    streamOut.Close();
    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
    string strResponse = streamIn.ReadToEnd();
    streamIn.Close();
于 2012-08-04T05:32:33.080 に答える
0

単体テストでのベスト プラクティスであるため、モッキングを使用することをお勧めします: c# での HTTP リクエストのユニット テストのスタックに関するこの回答を参照してください。

于 2012-08-04T01:56:03.370 に答える