3

我正在尝试通过 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);
4

2 回答 2

8

我没有收到错误消息,而是出现“无效登录”消息,这让我认为表单实际上发出了 Post 请求。无论如何,这是一个获取请求示例:

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());
    }
}

祝你好运!

于 2012-07-03T22:49:51.390 回答
6

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

var response = request.GetResponse() as HttpWebResponse;

这是它将实际从网络获取数据并为您提供任何相关错误的地方

于 2012-07-03T22:51:49.807 回答