-
October 2nd, 2010, 12:25 AM
#1
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;
-
October 2nd, 2010, 01:45 AM
#2
Re: Exception with HttpWebRequest for PUT method
Open that in a browser, or look at the header:
request.ContentType
That would cause an error
-
October 2nd, 2010, 09:00 AM
#3
Re: Exception with HttpWebRequest for PUT method
Originally Posted by dglienna
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
-
October 2nd, 2010, 09:03 AM
#4
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.
-
October 2nd, 2010, 01:15 PM
#5
Re: Exception with HttpWebRequest for PUT method
-
October 2nd, 2010, 01:41 PM
#6
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>
-
October 2nd, 2010, 02:00 PM
#7
Re: Exception with HttpWebRequest for PUT method
Originally Posted by mamin
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|