Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c# - GetResponse throws 400 Bad request

Getting 400 bad Request When trying to get the Response from my HTTPS post request. Here is my code:

try
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://coupons.valassis.eu/capi/directPrint/"+offerID);
    httpWebRequest.Credentials = new NetworkCredential(userName,Password);
    WebHeaderCollection myWebHeaderCollection = httpWebRequest.Headers;
    myWebHeaderCollection.Add("Authorization: Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(httpWebRequest.Credentials.ToString())));

    myWebHeaderCollection.Add("x-valassis-country-code: uk");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Accept = "application/json";
    httpWebRequest.Method = "POST";

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = "[{ "consumerId": "000000000000001", "remoteConsumerId": "000000000000001" , "Barcode": "Itf: 04910033400000000000000001,Ean13:ccode", "Type": "j", "returnUrl": "http://www.durex.co.uk","CouponDescription" : "Coupon For:"" + this.FirstName + " " + this.SurName + "" }]";

        var serializer = new JavaScriptSerializer();

        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (Stream streamReader =httpResponse.GetResponseStream())
        {
            using (StreamReader r = new StreamReader(streamReader))
            {
                var result = r.ReadToEnd();    

            }
        }
    }        
}
catch (WebException e)
{

}

Any one knows what might be the problem? or How to solve this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The JSON string that you create is invalid, as the CouponDescription property contains an odd number of quotes.

If i run this....

    var FirstName = "Joe";
var SurName = "Bloggs";
var json = "[{ "consumerId": "000000000000001", "remoteConsumerId": "000000000000001" , "Barcode": "Itf: 04910033400000000000000001,Ean13:ccode", "Type": "j", "returnUrl": "http://www.durex.co.uk","CouponDescription" : "Coupon For:"" + FirstName + " " + SurName + "" }]";

I get...

[{ "consumerId": "000000000000001", "remoteConsumerId": "000000000000001" , "Barcode": "Itf: 04910033400000000000000001,Ean13:ccode", "Type": "j", "returnUrl": "http://www.durex.co.uk","CouponDescription" : "Coupon For:"Joe Bloggs" }]

Look at the CouponFor value, the quotes are not closed.

Tools like LINQPad and JSONLint are very useful for validating code snippets in these scenarios


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...