通过ASP.NET C#发送HTTP请求

时间:2012-07-03 22:42:56

标签: c# asp.net http request

我试图通过C#发送HTTP Get请求。但它似乎不起作用。这是我的工作。而且我想从你那里获得使其运作所需的更正!

            String Mobile = txt_phone.Text;
            String Message = "You have registered successfuly.";
            Uri targetUri = new Uri("http://sms-om.com/smspro/sendsms.php?user=HatemSalem");
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(targetUri);

2 个答案:

答案 0 :(得分:7)

我没有收到错误,但是收到“无效登录”消息,这让我觉得该表单实际上会发出一个Post请求。以下是Get Request示例:

using System;
using System.Net;
using System.Text;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        byte[] buf = new byte[8192];

        //do get request
        HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create("http://sms-om.com/smspro/sendsms.php?user=HatemSalem");


        HttpWebResponse response = (HttpWebResponse)
            request.GetResponse();


        Stream resStream = response.GetResponseStream();

        string tempString = null;
        int count = 0;
        //read the data and print it
        do
        {
            count = resStream.Read(buf, 0, buf.Length);
            if (count != 0)
            {
                tempString = Encoding.ASCII.GetString(buf, 0, count);

                sb.Append(tempString);
            }
        }
        while (count > 0);
        Response.Write(sb.ToString());
    }
}
祝你好运!

答案 1 :(得分:4)

您目前尚未执行请求,请使用:

var response = request.GetResponse() as HttpWebResponse;

这是实际从网上获取数据并给你任何相关错误

的点