mentioned above:
In Webapi, it involves an important class, HttpWebRequest.
Learn Link:httpwebrequest Detailed
Sample Demo:
code example:
1. Front-End Code:
1 @{2 viewbag.title = "Index";3 Layout = null;4 }5@*<Scriptsrc= "~/views/js/jquery-1.7.1.js"></Script>*@6@*<Scriptsrc= "~/views/js/jquery.min.js"></Script>*@7 <Scriptsrc= "~/scripts/jquery-1.8.2.min.js"></Script>8 <H2>Index</H2>9Please enter your mobile phone number:<inputtype= "text"ID= "Inputphonenum"value=""/><BR/>Ten <inputtype= "button"value= "Get the place of attribution"ID= "Getphonedata"/> One <DivID= "Content"></Div> A <Script> - $(function () { - $('#getPhoneData'). Click (function () { the varStr= ""; - varInputphonenum= $("#inputPhoneNum"). Val (); - $.ajax ({ - URL:"/apitest/getphoneareadatet", + data: {phonenum:inputphonenum}, - DataType:"JSON", + Success:function(Result) { A $('#content'). empty (); //empty everything inside the restext. at varJSON=result; - varResobj=Json.parse (JSON); - //{province: ' Guangdong ', CityName: ' Guangzhou '} - $('#content'). HTML ("Mobile phone number" +Inputphonenum+ "the place of attribution is:" +resobj.province+resobj.cityname); - } - }); in }); - }); to </Script>
2, Backstage code:
1 Public classApitestcontroller:controller2 {3 //4 //GET:/apitest/5 6 Publicactionresult Index ()7 {8 returnView ();9 }Ten One PublicJsonresult Getphoneareadatet () { A //1.0 determine URL crawler (spider) - stringPhonenum = request.querystring["Phonenum"]; - stringURL ="Http://virtual.paipai.com/extinfo/GetMobileProductInfo"; the stringparas =string. Format ("mobile={0}&amount={1}&callname={2}" - , Phonenum -,10000 -,"Getphonenuminfoextcallback"); + stringResult =""; -HttpWebRequest request =(HttpWebRequest) webrequest.create (URL); +Request. Method ="POST"; A //make a request atRequest. ContentType ="application/x-www-form-urlencoded; charset=gb2312"; -Request. Maximumautomaticredirections =4; -Request. maximumResponseHeadersLength =4; - - byte[] Requestbytes =System.Text.Encoding.UTF8.GetBytes (paras); -Request. ContentLength =requestbytes.length; in -Stream Requeststream =request. GetRequestStream (); torequestStream.Write (Requestbytes,0, requestbytes.length); + requeststream.close (); - the //request. Credentials = CredentialCache.DefaultCredentials; *HttpWebResponse response =(HttpWebResponse) request. GetResponse (); $ Panax Notoginseng //Read Results -Stream Receivestream =Response. GetResponseStream (); theStreamReader Readstream =NewStreamReader (Receivestream, Encoding.GetEncoding ("gb2312")); +Result =readstream.readtoend (); A Response. Close (); the readstream.dispose (); + //{mobile: ' 13926231057 ', province: ' Guangdong ', ISP: ' China Mobile ', Stock: ' 1 ', Amount: ' 10000 ', Maxprice: ' 0 ', Minprice: ' 0 ', CityName : ' Guangzhou '} - $ intIndexofprofrist = Result.indexof ("Province")+Ten; $ intIndexofproend = Result.indexof ("ISP")-2; - intIndexofcityfrist = Result.lastindexof ("CityName")+Ten; - intIndexofcityend = Result.indexof ('}')-1; the stringStrpro = result.substring (indexofprofrist,indexofproend-indexofprofrist); - stringstrcity = result.substring (indexofcityfrist, Indexofcityend-indexofcityfrist);Wuyi stringStrjsobj ="{\ "province\": \ ""+ Strpro +"\ ", \" cityname\ ": \""+ strcity +"\"}"; the //int Indexofname - returnJson (Strjsobj, jsonrequestbehavior.allowget); Wu } -}
Use WEBAPI to get cell phone number attribution