简体   繁体   中英

Call web service with parameters

I have to call a web service. The web service has input two parameters and does not return anything in output.

Can you tell me if the code (written below) is correct and complete? I inserted it inside the main.

var httpWebRequest = (HttpWebRequest)WebRequest.Create("url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential("user", "pwd", "domain");

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{p1:\'Hello\'";

    string tmpjs = ",p2:\'world\'}";                       
    json = json + tmpjs;

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

HttpWebResponse httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();

It would be easier for to Add service reference and call your Web Service Methods.

Then you can access the service like this .

Your code would look like this:

string json = "{p1:\'Hello\'";                              

string tmpjs = ",p2:\'world\'}";   
json = json + tmpjs;

using(YourService service = new YourService()) 
{
    service.Credentials = new NetworkCredential("user", "pwd", "domain");
    serivce.YourServiceMethod(json, param2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM