views:

12

answers:

0

Hi all, I'm trying to use server to server XML API for google checkout with c#.net 3.5.

I'm getting the following error when I try and post my URL "The remote server returned an error: (400) Bad Request." even though I'm sure I've followed their instructions to the letter. My code is below... help anyone?

    public void DoWebRequest(string content)
    {
        //Save the content string for later use
        _checkoutString = content;

        // Create a new request to the mentioned URL.    
        WebRequest myWebRequest = WebRequest.Create("https://sandbox.google.com/checkout/api/checkout/v2/merchantCheckout/Merchant/" + Utils.MerchantData.Id);

        //Create authorisation key
        string encodeKey = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(Utils.MerchantData.Id + ":" + Utils.MerchantData.Key));

        // Create an instance of the RequestState and assign 'myWebRequest' to it's request field.    
        RequestState myRequestState = new RequestState();
        myRequestState.request = myWebRequest;
        myWebRequest.ContentType = "application/xml; charset=UTF-8";
        //myWebRequest.Accept = 
        //myWebRequest.Headers["Accept"] = "application/xml; charset=UTF-8";
        ((HttpWebRequest)myWebRequest).Accept = "application/xml; charset=UTF-8";
        myWebRequest.Headers.Add("Authorization", "Basic " + encodeKey);

        // Set the 'Method' property  to 'POST' to post data to a Uri.
        myRequestState.request.Method = "POST";

        // Start the Asynchronous 'BeginGetRequestStream' method call.    
        IAsyncResult r = (IAsyncResult)myWebRequest.BeginGetRequestStream(new AsyncCallback(ReadCallback), myRequestState);

        // Pause the current thread until the async operation completes.
        _allDone.WaitOne();

        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse myWebResponse = myWebRequest.GetResponse();
        Stream streamResponse = myWebResponse.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);

        while (count > 0)
        {
            String outputData = new String(readBuff, 0, count);
            Debug.Write(outputData);
            count = streamRead.Read(readBuff, 0, 256);
        }

        // Close the Stream Object.
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse Resource.
        myWebResponse.Close();      
    }

    private static void ReadCallback(IAsyncResult asynchronousResult)
    {
        RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
        WebRequest myWebRequest = myRequestState.request;

        // End the Asynchronus request.
        Stream streamResponse = myWebRequest.EndGetRequestStream(asynchronousResult);

        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(_checkoutString);

        // Write the data to the stream.
        streamResponse.Write(byteArray, 0, _checkoutString.Length);
        streamResponse.Close();
        _allDone.Set();
    }

I get the error on the following line:

        WebResponse myWebResponse = myWebRequest.GetResponse();

My "content" string is as follows:

            string googleCheckoutString =
            "<!-- Sell physical goods with tax and shipping -->" +
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<checkout-shopping-cart xmlns=\"http://checkout.google.com/schema/2\"&gt;" +
            "<shopping-cart>" +
            "<items>" +
            products +
            "</items>" +
            "</shopping-cart>" +
            "<checkout-flow-support>" +
            "<merchant-checkout-flow-support>" +
            "<shipping-methods>" +
            shipping +
            "</shipping-methods>" +
            "</merchant-checkout-flow-support>" +
            "</checkout-flow-support>" +
            "</checkout-shopping-cart>";