转换CURL" -i --user"到C#HttpWebRequest

时间:2017-07-12 06:39:24

标签: c# curl httpwebrequest plivo

我想运行CURL POST以通过Plivo API服务发送SMS消息。

我是通过在C#中使用HttpWebRequest来实现的(见下文)。

参数如下: POSTurl =" https://api.plivo.com/v1/Account/MA **************** M2 /消息/'“ JSONRequest ==" src = + 972545675453& dst = 0545675453& text = TEST“

在他们的文档中,我需要使用以下参数:  -i --user AUTH_ID:AUTH_TOKEN

我不知道如何实现这一点,但它不起作用(401错误 - 无法说明)
我尝试在我的代码中使用它代替auth参数,如下所示: auth ==“MA **************** MM2:Yj ************************** M5ZjA4"

需要一些建议

由于

这是我的代码:

public static SqlString POSTSG(String POSTurl, String auth, SqlString JSONRequest)
{
    SqlPipe pipe = SqlContext.Pipe;



    //Create Request
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(POSTurl);
    myHttpWebRequest.Method = "POST";


    myHttpWebRequest.Headers.Add("Authorization", auth);
    myHttpWebRequest.ContentType = "application/json; charset=utf-8";
    StreamWriter streamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream(), System.Text.Encoding.UTF8);
    streamWriter.Write(JSONRequest);
    streamWriter.Flush();
    streamWriter.Close();

    // Get the response
    HttpWebResponse httpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

    // Create a new read stream for the response body and read it
    StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream());
    SqlString result = streamReader.ReadToEnd();


    return (result);





}

1 个答案:

答案 0 :(得分:0)

需要发送用户:传递为网络凭据而不是身份验证。并使用' application / json'作为内容类型。

[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlString POST(String POSTurl, String auth,String user, String pass, String ContentType, SqlString JSONRequest)
{
    SqlPipe pipe = SqlContext.Pipe;



    //Create Request
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(POSTurl);
    myHttpWebRequest.Method = "POST";
    if (user != null)   { myHttpWebRequest.Credentials = new NetworkCredential(user, pass); }
    else if (auth!=null) { myHttpWebRequest.Headers.Add("Authorization", auth); }
    myHttpWebRequest.ContentType = ContentType;
    StreamWriter streamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream());
    streamWriter.Write(JSONRequest);
    streamWriter.Flush();
    streamWriter.Close();

    // Get the response
    HttpWebResponse httpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

    // Create a new read stream for the response body and read it
    StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);
    SqlString result = streamReader.ReadToEnd();



    return (result);

}