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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11320240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:23:52  来源:igfitidea点击:

Send HTTP Request via ASP.NET C#

c#asp.nethttprequest

提问by Aan

Am trying to send HTTP Get request via C#. But it seems not working. Here is my work. And i wanna from you the corrections needed to make it work !

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

采纳答案by Not loved

You currently haven't executed the request, use:

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

var response = request.GetResponse() as HttpWebResponse;

This is the point where it will actually get data from the web and give you any relevent errors

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

回答by Hanlet Esca?o

I do not get an error, but an "Invalid Login" message, which makes me think the form actually makes a Post request instead. Here is a Get Request example anyways:

我没有收到错误消息,而是收到“无效登录”消息,这让我认为该表单实际上是在发出 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());
    }
}

Good luck!

祝你好运!