Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:52:58+00:00 2026-06-03T22:52:58+00:00

You would think with all the posts here that this would be easy to

  • 0

You would think with all the posts here that this would be easy to figure out. 😐 Well here is what should be a simple example. NOTE The web service is VB and the client is c#. The wb service sends and receives fine when called from JQuery. From .NET There is a problem,
If the service asks for a parameter as show below then the client’s getresponse method gets error 500 Internal server error

The Web Service

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
Public Function Test(WebInfo As GetUserID) As Person
    Dim Someone As New Person
    Someone.Name = "Bob"
    Someone.FavoriteColor = "Green"
    Someone.ID = WebInfo.WebUserID.ToString()
    Return Someone
End Function

The Web Client (set up to be send and receive JSON)

    public Person Test(int UserID, string url) {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + "test.asmx/Test");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/json; charset=utf-8";
        StreamWriter sw = new StreamWriter(webRequest.GetRequestStream());
        sw.Write("{'WebInfo':{'WebUserID':1}}");  // this works from JQuery
        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
        Stream responseStream = webResponse.GetResponseStream();
        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Person));
        Person someone = (Person)jsonSerializer.ReadObject(responseStream);
        return someone;
    }

Has anyone out there done this successfully?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:53:00+00:00Added an answer on June 3, 2026 at 10:53 pm

    Here is a method that makes calls to a JSON web service, allowing the developer to both send and receive complext data types. The object passed in can be any data type or class. The result is a JSON string, and or any error message the methods type is shown below

    public class WebServiceCallReturn {
        public string JSONResponse { get; set; }
        public string SimpleResponse { get; set; }
        public string Error { get; set; }
    }
    
    public WebServiceCallReturn WebServiceJSONCall(string uri, string requestType, object postData = null) {
        WebServiceCallReturn result = new WebServiceCallReturn();
    
        // create request
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
        webRequest.ContentType = "application/json; charset=utf-8";
        webRequest.Method = requestType;
        webRequest.Accept = "application/json; charset=utf-8";
        // add json data object to send
        if (requestType == "POST") {
            string json = "{ }";
            if (postData != null) {
                try {   // the serializer is fairly robust when used this way
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(postData.GetType());
                    MemoryStream ms = new MemoryStream();
                    ser.WriteObject(ms, postData);
                    json = Encoding.UTF8.GetString(ms.ToArray());
                } catch {
                    result.Error = "Error serializing post";
                }
            }
            webRequest.ContentLength = json.Length;
            StreamWriter sw;
            try {
                sw = new StreamWriter(webRequest.GetRequestStream());
            } catch (Exception ex) {
                // the remote name could not be resolved
                result.Error = ex.Message;
                return result;
            }
            sw.Write(json);
            sw.Close();
        }
    
        // read response
        HttpWebResponse webResponse;
        try {
            webResponse = (HttpWebResponse)webRequest.GetResponse();
        } catch (Exception ex) {
            // The remote server returned an error...
            // (400) Bad Request
            // (403) Access forbidden   (check the application pool)
            // (404) Not Found
            // (405) Method not allowed
            // (415) ...not the expected type
            // (500) Internal Server Error   (problem with IIS or unhandled error in web service)
            result.Error = ex.Message;
            return result;
        }
        Stream responseStream = webResponse.GetResponseStream();
        StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
        string resultString = sr.ReadToEnd();
        sr.Close();
        responseStream.Close();
        result.JSONResponse = resultString;
        return result;
    }
    

    This method could be used as follows

    public SomeCustomDataClass Getsomeinformation(int userID) {
    
        UserInfoClass postData = new UserInfoClass();
        postData.WebUserID = userID;
        SomeCustomDataClass result = new SomeCustomDataClass();
    
        string uri = URL + "SomeServices.svc/GetSomething";
        WebServiceCallReturn webReturn = WebServiceJSONCall(uri, "POST", postData);
        if (webReturn.Error == null) {
            //resultString = CleanJSON(resultString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            try {
                result = serializer.Deserialize<SomeCustomDataClass>(webReturn.JSONResponse);
            } catch {
                result.Error = "Error deserializing";
            }
        } else {
            result.Error = webReturn.Error;
        }
        return result;
    }
    

    Hope that helps someone

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You would think that launching a bat file from Java would be an easy
This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]
How can I force validation when user clicks button? One would think this should
First of all, I've looked at EVERY other post here, about this (I think)
First, I would to thank everyone for all the help they provide via this
My problem is one that you would think is quite common, but I haven't
Sorry if this is duplicate,I would think it would be but couldn't find anything.
From what I've read from Herb Sutter and others you would think that volatile
The following code is an example of what I think would qualify as pseudocode,
Here's what I want to accomplish: Select all rows from table posts while also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.