CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    5

    Exception with HttpWebRequest for PUT method

    Hi,
    I am receiving below error with my code

    System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type.
    at System.Net.HttpWebRequest.GetResponse()

    My code is as below:

    Byte[] bytes = UTF8Encoding.UTF8.GetBytes((string)xmldata);
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = "PUT";
    //request.ContentType = "text/xml";
    request.ContentType = "application/atom+xml; charset=UTF-8; type=entry";
    request.ContentLength = bytes.Length;
    request.KeepAlive = true;
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(bytes, 0, bytes.Length);
    dataStream.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //==>failing on this line
    string returnString = response.StatusCode.ToString();
    return returnString;

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Exception with HttpWebRequest for PUT method

    Open that in a browser, or look at the header:

    request.ContentType


    That would cause an error
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2010
    Posts
    5

    Re: Exception with HttpWebRequest for PUT method

    Quote Originally Posted by dglienna View Post
    Open that in a browser, or look at the header:

    request.ContentType


    That would cause an error
    Thank you for your answer. you are right, issue apears to be in request.ContentType. But I am not sure what else I should be using instead of

    request.ContentType = "application/atom+xml; charset=UTF-8; type=entry";

    I tried request.ContentType = "text/xml"; as well and same error.

    Since method required is PUT method I cannot try in browser unless somthing i do not know. I have code working in ASP but I am trying to do the same in C#. Actual XML data and URL is working fine since I know it is working in ASP. Something wrong with the way C# code is written and not sure what.


    Any help will be greatly appreciated. Let me know if you need any other information

  4. #4
    Join Date
    Oct 2010
    Posts
    5

    Re: Exception with HttpWebRequest for PUT method

    I forgot to mention one thing url has site name + parameter. Can this be a issue ?

    e.g. url is https://www.sitename.com/SellerPorta...ord=mypassword

    for ASP it works fine using xmlhttp method when i pass exact same url.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Exception with HttpWebRequest for PUT method

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Oct 2010
    Posts
    5

    Re: Exception with HttpWebRequest for PUT method

    Thank for Detailed documentation. I am not able to figure out where is the exact issue is.

    Here is the format of XML I am posting using PUT Method:

    <?xml version="1.0" encoding="utf-8"?>
    <store-inventory xmlns="http://sitename.com/catalog/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sitename.com/catalog/v2 http://sitename.com/SellerPortal/s/schema/rest/inventory/import/v2/store-inventory.xsd">
    <item item-id="3m15577">
    <locations>
    <location location-id="200150693">
    <quantity>12</quantity>
    <pick-up-now-eligible>true</pick-up-now-eligible>
    </location>
    </locations>
    </item>
    </store-inventory>

  7. #7
    Join Date
    Oct 2010
    Posts
    5

    Re: Exception with HttpWebRequest for PUT method

    Quote Originally Posted by mamin View Post
    Thank for Detailed documentation. I am not able to figure out where is the exact issue is.

    Here is the format of XML I am posting using PUT Method:

    <?xml version="1.0" encoding="utf-8"?>
    <store-inventory xmlns="http://sitename.com/catalog/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sitename.com/catalog/v2 http://sitename.com/SellerPortal/s/schema/rest/inventory/import/v2/store-inventory.xsd">
    <item item-id="3m15577">
    <locations>
    <location location-id="200150693">
    <quantity>12</quantity>
    <pick-up-now-eligible>true</pick-up-now-eligible>
    </location>
    </locations>
    </item>
    </store-inventory>
    I was able to make this work by changing below two line:

    request.ContentType = "application/xml";
    request.Accept = "application/xml";

    After this change, it worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured