Csharp:HttpWebRequest
程序员文章站
2022-06-10 17:50:13
/// /// Define other methods and classes here /// /// /// /// public static Task MakeAsyncRequest(string contentType, string contenttxt, ... ......
/// <summary>
/// define other methods and classes here
/// </summary>
/// <param name="url"></param>
/// <param name="contenttype"></param>
/// <returns></returns>
public static task<string> makeasyncrequest(string contenttype, string contenttxt, string mobile)
{
string url = "http://www.dusystem.com/userserviceapi";
httpwebrequest request = (httpwebrequest)webrequest.create(url);
request.contenttype = contenttype;
request.method = webrequestmethods.http.post; //get
request.timeout = 20000;
request.proxy = null;
byte[] data = system.text.encoding.getencoding("gbk").getbytes("geovindu"); //system.text.unicodeencoding.ascii.getbytes("geovindu");//utf8
base64encoder myencoder = new base64encoder(data);
stringbuilder sb = new stringbuilder();
sb.append(myencoder.getencoded());
string msg = urlencode(contenttxt);
string content = string.format("method={0}&islongsms={1}&username={2}&password={3}&smstype={4}&mobile={5}&content={6}", "ms", 0, "geovindu", sb.tostring(), 0, mobile, msg);//
byte[] bytes = encoding.getencoding("gbk").getbytes(content);
request.contentlength = bytes.length;
stream os = request.getrequeststream();
//req.getresponseasync();
os.write(bytes, 0, bytes.length);
os.close();
//system.net.webresponse resp = req.getresponse();
webresponse resp = request.getresponse();
task<webresponse> task = task.factory.fromasync(
request.begingetresponse,
asyncresult => request.endgetresponse(asyncresult),
(object)null);
return task.continuewith(t => readstreamfromresponse(resp, content));
}
/// <summary>
/// 对内容进行编码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string urlencode(string str)
{
stringbuilder sb = new stringbuilder();
byte[] bystr = system.text.encoding.getencoding("gbk").getbytes(str); //默认是system.text.encoding.default.getbytes(str)
for (int i = 0; i < bystr.length; i++)
{
sb.append(@"%" + convert.tostring(bystr[i], 16));
}
return (sb.tostring());
}
/// <summary>
///
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
private static string readstreamfromresponse(webresponse response, string content)
{
stream responsestream = response.getresponsestream();
using (streamreader sr = new streamreader(responsestream))
{
string strcontent = sr.readtoend();
return strcontent;
}
}
delegate string synchoperation(string value);
/// <summary>
///
/// </summary>
/// <param name="callback"></param>
/// <param name="value"></param>
void beginthesynchronousoperation(asynccallback callback, string value)
{
synchoperation op = new synchoperation(synchronousoperation);
op.begininvoke(value, callback, op);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
string synchronousoperation(string value)
{
string str = "geovindu";
thread.sleep(10000);
str = str + value;
return str;
}
/// <summary>
///
/// </summary>
/// <param name="result"></param>
void callbackoperation(iasyncresult result)
{
// get your delegate
var ar = result.asyncstate as synchoperation;
// end invoke and get value
var returned = ar.endinvoke(result);
response.write(returned);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void button1_click(object sender, eventargs e)
{
beginthesynchronousoperation(callbackoperation, this.textbox1.text.trim());
var task = makeasyncrequest("application/x-www-form-urlencoded", "geovindu", "1388888888");
response.write(string.format("got response of {0}", task.result)); //返回成功
}
上一篇: 文件下载用ajax请求后没响应
推荐阅读
-
C#使用HttpWebRequest与HttpWebResponse模拟用户登录
-
C#语法糖(Csharp Syntactic sugar)大汇总
-
.NET Core CSharp 中级篇2-8 特性标签
-
C#基于HttpWebRequest实现发送HTTP请求的方法分析
-
c# HttpWebRequest通过代理服务器抓取网页内容应用介绍
-
CSharp初级篇 1-4 this、索引器、静态、常量以及只读
-
.NET Core CSharp初级篇 1-1
-
使用HttpWebRequest向网站模拟上传数据
-
WEB上调用HttpWebRequest奇怪问题的解决方法
-
.NET Core CSharp 中级篇 2-2 List,ArrayList和Dictionary