C # httpwebreuqest post pass parameter JSON

Source: Internet
Author: User


public static string Getmobileconfbyuserid (string userid)
{
String rs = null;
if (!string. IsNullOrEmpty (userid))
{
if (string. IsNullOrEmpty (Stronline))
{
Stronline = configurationmanager.appsettings["Onlineurl"];
}
Servicepointmanager.defaultconnectionlimit = 300;
System.GC.Collect ();
Cookiecontainer Cookiecontainer = new Cookiecontainer ();
Set up related parameters for submission
HttpWebRequest request = null;
HttpWebResponse sendsmsresponse = null;
Stream dataStream = null;
StreamReader sendsmsresponsestream = null;
Try
{
Request = WebRequest.Create (stronline) as HttpWebRequest;
Request. method = "POST";
Request. KeepAlive = false;
Request. Servicepoint.connectionlimit = 300;
Request. AllowAutoRedirect = true;
Request. Timeout = 10000;
Request. Readwritetimeout = 10000;
Request. ContentType = "Application/json";
Request. Accept = "Application/xml";
Request. Headers.add ("X-auth-token", Httputility.urlencode ("OpenStack"));
String strcontent = "{\ appid\": \ "\", \ "method\": \ "\" \ "tokenid\": \ "\", \ "data\": {\ "useraccountid\": \ "" + userid + "\"} }";
byte[] bytes = Encoding.UTF8.GetBytes (strcontent);
Request. Proxy = null;
Request. Cookiecontainer = Cookiecontainer;
using (DataStream = Request. GetRequestStream ())
{
Datastream.write (bytes, 0, bytes. Length);
}
Sendsmsresponse = (httpwebresponse) request. GetResponse ();
if (Sendsmsresponse.statuscode = = httpstatuscode.requesttimeout)
{
if (sendsmsresponse!= null)
{
Sendsmsresponse.close ();
Sendsmsresponse = null;
}
if (Request!= null)
{
Request. Abort ();
}
return null;
}
Sendsmsresponsestream = new StreamReader (Sendsmsresponse.getresponsestream (), encoding.getencoding ("Utf-8"));
String strrespone = Sendsmsresponsestream.readtoend ();

return strrespone;
}
catch (Exception ex)
{

if (DataStream!= null)
{
Datastream.close ();
Datastream.dispose ();
DataStream = null;
}
if (Sendsmsresponsestream!= null)
{
Sendsmsresponsestream.close ();
Sendsmsresponsestream.dispose ();
Sendsmsresponsestream = null;
}
if (sendsmsresponse!= null)
{
Sendsmsresponse.close ();
Sendsmsresponse = null;
}
if (Request!= null)
{
Request. Abort ();
}
}
Finally
{
if (DataStream!= null)
{
Datastream.close ();
Datastream.dispose ();
DataStream = null;
}
if (Sendsmsresponsestream!= null)
{
Sendsmsresponsestream.close ();
Sendsmsresponsestream.dispose ();
Sendsmsresponsestream = null;
}
if (sendsmsresponse!= null)
{
Sendsmsresponse.close ();
Sendsmsresponse = null;
}
if (Request!= null)
{
Request. Abort ();
}
}
}
Return RS;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.