Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...