Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET REST Client
i am using a web service created in java in anothe rmachine and consuming it using rest client in C#.net while doing that i am getting an error saying "the remote server returned an error:unsupported media type 415"
 
...please fix my problem... i am using a console application and the code is as follows
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Web;
using System.Net;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            do
            {
                try
                {
                    string content;
                   // Console.WriteLine("Enter Method:");
                    string Method = "POST";
                        //Console.ReadLine();

                   // Console.WriteLine("Enter URI:");
                    string uri = "http://172.18.19.171:8080/RestDrools/rest/getPerson/personXML";
                    Console.WriteLine(uri);
                    HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
                    req.KeepAlive = false;
                    req.Method = Method.ToUpper();
 
                    if (("POST,PUT").Split(',').Contains(Method.ToUpper()))
                    {
                       // Console.WriteLine("Enter XML FilePath:");
                        string FilePath = "D://xyz.xml";
                        Console.WriteLine(FilePath);
                            //Console.ReadLine();
                        content = (File.OpenText(@FilePath)).ReadToEnd();
 
                        byte[] buffer = Encoding.ASCII.GetBytes(content);
                        req.ContentLength = buffer.Length;
                        req.ContentType = "applicaton/xml";
                        Stream PostData = req.GetRequestStream();
                        PostData.Write(buffer, 0, buffer.Length);
                        PostData.Close();
                    }
 
                    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
 
                    Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    StreamReader loResponseStream =
                    new StreamReader(resp.GetResponseStream(), enc);
 
                    string Response = loResponseStream.ReadToEnd();
 
                    loResponseStream.Close();
                    resp.Close();
                    Console.WriteLine(Response);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                }
 
                Console.WriteLine();
                Console.WriteLine("Do you want to continue?");
            } while (Console.ReadLine().ToUpper() == "Y");
        }
    }
}
 
[edit]Code block added - OriginalGriff[/edit]
Posted 6-Nov-12 1:18am
Edited 6-Nov-12 1:32am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100